4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "hw/virtio.h"
16 #include "qemu_socket.h"
17 #include "hw/virtio-pci.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "virtio-9p-debug.h"
21 #include "virtio-9p-xattr.h"
22 #include "virtio-9p-coth.h"
38 static int omode_to_uflags(int8_t mode)
72 void cred_init(FsCred *credp)
80 static void v9fs_string_init(V9fsString *str)
86 static void v9fs_string_free(V9fsString *str)
93 static void v9fs_string_null(V9fsString *str)
95 v9fs_string_free(str);
98 static int number_to_string(void *arg, char type)
100 unsigned int ret = 0;
104 unsigned int num = *(unsigned int *)arg;
113 unsigned long num = *(unsigned long *)arg;
121 printf("Number_to_string: Unknown number format\n");
128 static int GCC_FMT_ATTR(2, 0)
129 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
132 char *iter = (char *)fmt;
136 unsigned int arg_uint;
137 unsigned long arg_ulong;
139 /* Find the number of %'s that denotes an argument */
140 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
145 len = strlen(fmt) - 2*nr_args;
155 /* Now parse the format string */
156 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
160 arg_uint = va_arg(ap2, unsigned int);
161 len += number_to_string((void *)&arg_uint, 'u');
164 if (*++iter == 'u') {
165 arg_ulong = va_arg(ap2, unsigned long);
166 len += number_to_string((void *)&arg_ulong, 'U');
172 arg_char_ptr = va_arg(ap2, char *);
173 len += strlen(arg_char_ptr);
180 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
187 *strp = g_malloc((len + 1) * sizeof(**strp));
189 return vsprintf(*strp, fmt, ap);
192 static void GCC_FMT_ATTR(2, 3)
193 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
198 v9fs_string_free(str);
201 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
208 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
210 v9fs_string_free(lhs);
211 v9fs_string_sprintf(lhs, "%s", rhs->data);
215 * Return TRUE if s1 is an ancestor of s2.
217 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
218 * As a special case, We treat s1 as ancestor of s2 if they are same!
220 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
222 if (!strncmp(s1->data, s2->data, s1->size)) {
223 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
230 static size_t v9fs_string_size(V9fsString *str)
235 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
239 for (f = s->fid_list; f; f = f->next) {
248 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
252 f = lookup_fid(s, fid);
257 f = g_malloc0(sizeof(V9fsFidState));
260 f->fid_type = P9_FID_NONE;
262 f->next = s->fid_list;
268 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
272 if (fidp->fs.xattr.copied_len == -1) {
273 /* getxattr/listxattr fid */
277 * if this is fid for setxattr. clunk should
278 * result in setxattr localcall
280 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
281 /* clunk after partial write */
285 if (fidp->fs.xattr.len) {
286 retval = v9fs_co_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
287 fidp->fs.xattr.value,
289 fidp->fs.xattr.flags);
291 retval = v9fs_co_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
294 v9fs_string_free(&fidp->fs.xattr.name);
296 if (fidp->fs.xattr.value) {
297 g_free(fidp->fs.xattr.value);
302 static int free_fid(V9fsState *s, int32_t fid)
305 V9fsFidState **fidpp, *fidp;
307 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
308 if ((*fidpp)->fid == fid) {
313 if (*fidpp == NULL) {
320 if (fidp->fid_type == P9_FID_FILE) {
321 retval = v9fs_co_close(s, fidp);
322 } else if (fidp->fid_type == P9_FID_DIR) {
323 retval = v9fs_co_closedir(s, fidp);
324 } else if (fidp->fid_type == P9_FID_XATTR) {
325 retval = v9fs_xattr_fid_clunk(s, fidp);
327 v9fs_string_free(&fidp->path);
332 #define P9_QID_TYPE_DIR 0x80
333 #define P9_QID_TYPE_SYMLINK 0x02
335 #define P9_STAT_MODE_DIR 0x80000000
336 #define P9_STAT_MODE_APPEND 0x40000000
337 #define P9_STAT_MODE_EXCL 0x20000000
338 #define P9_STAT_MODE_MOUNT 0x10000000
339 #define P9_STAT_MODE_AUTH 0x08000000
340 #define P9_STAT_MODE_TMP 0x04000000
341 #define P9_STAT_MODE_SYMLINK 0x02000000
342 #define P9_STAT_MODE_LINK 0x01000000
343 #define P9_STAT_MODE_DEVICE 0x00800000
344 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
345 #define P9_STAT_MODE_SOCKET 0x00100000
346 #define P9_STAT_MODE_SETUID 0x00080000
347 #define P9_STAT_MODE_SETGID 0x00040000
348 #define P9_STAT_MODE_SETVTX 0x00010000
350 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
351 P9_STAT_MODE_SYMLINK | \
352 P9_STAT_MODE_LINK | \
353 P9_STAT_MODE_DEVICE | \
354 P9_STAT_MODE_NAMED_PIPE | \
357 /* This is the algorithm from ufs in spfs */
358 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
362 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
363 memcpy(&qidp->path, &stbuf->st_ino, size);
364 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
366 if (S_ISDIR(stbuf->st_mode)) {
367 qidp->type |= P9_QID_TYPE_DIR;
369 if (S_ISLNK(stbuf->st_mode)) {
370 qidp->type |= P9_QID_TYPE_SYMLINK;
374 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
379 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
383 stat_to_qid(&stbuf, qidp);
387 static V9fsPDU *alloc_pdu(V9fsState *s)
391 if (!QLIST_EMPTY(&s->free_list)) {
392 pdu = QLIST_FIRST(&s->free_list);
393 QLIST_REMOVE(pdu, next);
398 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
404 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
408 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
409 size_t offset, size_t size, int pack)
414 for (i = 0; size && i < sg_count; i++) {
416 if (offset >= sg[i].iov_len) {
418 offset -= sg[i].iov_len;
421 len = MIN(sg[i].iov_len - offset, size);
423 memcpy(sg[i].iov_base + offset, addr, len);
425 memcpy(addr, sg[i].iov_base + offset, len);
440 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
442 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
446 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
449 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
453 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
457 struct iovec *src_sg;
461 src_sg = pdu->elem.in_sg;
462 num = pdu->elem.in_num;
464 src_sg = pdu->elem.out_sg;
465 num = pdu->elem.out_num;
469 for (i = 0; i < num; i++) {
471 sg[j].iov_base = src_sg[i].iov_base;
472 sg[j].iov_len = src_sg[i].iov_len;
474 } else if (offset < (src_sg[i].iov_len + pos)) {
475 sg[j].iov_base = src_sg[i].iov_base;
476 sg[j].iov_len = src_sg[i].iov_len;
477 sg[j].iov_base += (offset - pos);
478 sg[j].iov_len -= (offset - pos);
481 pos += src_sg[i].iov_len;
487 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
489 size_t old_offset = offset;
494 for (i = 0; fmt[i]; i++) {
497 uint8_t *valp = va_arg(ap, uint8_t *);
498 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
503 valp = va_arg(ap, uint16_t *);
504 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
505 *valp = le16_to_cpu(val);
510 valp = va_arg(ap, uint32_t *);
511 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
512 *valp = le32_to_cpu(val);
517 valp = va_arg(ap, uint64_t *);
518 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
519 *valp = le64_to_cpu(val);
523 struct iovec *iov = va_arg(ap, struct iovec *);
524 int *iovcnt = va_arg(ap, int *);
525 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
529 V9fsString *str = va_arg(ap, V9fsString *);
530 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
531 /* FIXME: sanity check str->size */
532 str->data = g_malloc(str->size + 1);
533 offset += pdu_unpack(str->data, pdu, offset, str->size);
534 str->data[str->size] = 0;
538 V9fsQID *qidp = va_arg(ap, V9fsQID *);
539 offset += pdu_unmarshal(pdu, offset, "bdq",
540 &qidp->type, &qidp->version, &qidp->path);
544 V9fsStat *statp = va_arg(ap, V9fsStat *);
545 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
546 &statp->size, &statp->type, &statp->dev,
547 &statp->qid, &statp->mode, &statp->atime,
548 &statp->mtime, &statp->length,
549 &statp->name, &statp->uid, &statp->gid,
550 &statp->muid, &statp->extension,
551 &statp->n_uid, &statp->n_gid,
556 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
557 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
558 &iattr->valid, &iattr->mode,
559 &iattr->uid, &iattr->gid, &iattr->size,
560 &iattr->atime_sec, &iattr->atime_nsec,
561 &iattr->mtime_sec, &iattr->mtime_nsec);
571 return offset - old_offset;
574 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
576 size_t old_offset = offset;
581 for (i = 0; fmt[i]; i++) {
584 uint8_t val = va_arg(ap, int);
585 offset += pdu_pack(pdu, offset, &val, sizeof(val));
590 cpu_to_le16w(&val, va_arg(ap, int));
591 offset += pdu_pack(pdu, offset, &val, sizeof(val));
596 cpu_to_le32w(&val, va_arg(ap, uint32_t));
597 offset += pdu_pack(pdu, offset, &val, sizeof(val));
602 cpu_to_le64w(&val, va_arg(ap, uint64_t));
603 offset += pdu_pack(pdu, offset, &val, sizeof(val));
607 struct iovec *iov = va_arg(ap, struct iovec *);
608 int *iovcnt = va_arg(ap, int *);
609 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
613 V9fsString *str = va_arg(ap, V9fsString *);
614 offset += pdu_marshal(pdu, offset, "w", str->size);
615 offset += pdu_pack(pdu, offset, str->data, str->size);
619 V9fsQID *qidp = va_arg(ap, V9fsQID *);
620 offset += pdu_marshal(pdu, offset, "bdq",
621 qidp->type, qidp->version, qidp->path);
625 V9fsStat *statp = va_arg(ap, V9fsStat *);
626 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
627 statp->size, statp->type, statp->dev,
628 &statp->qid, statp->mode, statp->atime,
629 statp->mtime, statp->length, &statp->name,
630 &statp->uid, &statp->gid, &statp->muid,
631 &statp->extension, statp->n_uid,
632 statp->n_gid, statp->n_muid);
636 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
637 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
638 statp->st_result_mask,
639 &statp->qid, statp->st_mode,
640 statp->st_uid, statp->st_gid,
641 statp->st_nlink, statp->st_rdev,
642 statp->st_size, statp->st_blksize, statp->st_blocks,
643 statp->st_atime_sec, statp->st_atime_nsec,
644 statp->st_mtime_sec, statp->st_mtime_nsec,
645 statp->st_ctime_sec, statp->st_ctime_nsec,
646 statp->st_btime_sec, statp->st_btime_nsec,
647 statp->st_gen, statp->st_data_version);
656 return offset - old_offset;
659 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
661 int8_t id = pdu->id + 1; /* Response */
667 if (s->proto_version != V9FS_PROTO_2000L) {
670 str.data = strerror(err);
671 str.size = strlen(str.data);
673 len += pdu_marshal(pdu, len, "s", &str);
677 len += pdu_marshal(pdu, len, "d", err);
679 if (s->proto_version == V9FS_PROTO_2000L) {
684 /* fill out the header */
685 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
687 /* keep these in sync */
691 /* push onto queue and notify */
692 virtqueue_push(s->vq, &pdu->elem, len);
694 /* FIXME: we should batch these completions */
695 virtio_notify(&s->vdev, s->vq);
700 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
705 if (mode & P9_STAT_MODE_DIR) {
709 if (mode & P9_STAT_MODE_SYMLINK) {
712 if (mode & P9_STAT_MODE_SOCKET) {
715 if (mode & P9_STAT_MODE_NAMED_PIPE) {
718 if (mode & P9_STAT_MODE_DEVICE) {
719 if (extension && extension->data[0] == 'c') {
730 if (mode & P9_STAT_MODE_SETUID) {
733 if (mode & P9_STAT_MODE_SETGID) {
736 if (mode & P9_STAT_MODE_SETVTX) {
743 static int donttouch_stat(V9fsStat *stat)
745 if (stat->type == -1 &&
747 stat->qid.type == -1 &&
748 stat->qid.version == -1 &&
749 stat->qid.path == -1 &&
753 stat->length == -1 &&
760 stat->n_muid == -1) {
767 static void v9fs_stat_free(V9fsStat *stat)
769 v9fs_string_free(&stat->name);
770 v9fs_string_free(&stat->uid);
771 v9fs_string_free(&stat->gid);
772 v9fs_string_free(&stat->muid);
773 v9fs_string_free(&stat->extension);
776 static uint32_t stat_to_v9mode(const struct stat *stbuf)
780 mode = stbuf->st_mode & 0777;
781 if (S_ISDIR(stbuf->st_mode)) {
782 mode |= P9_STAT_MODE_DIR;
785 if (S_ISLNK(stbuf->st_mode)) {
786 mode |= P9_STAT_MODE_SYMLINK;
789 if (S_ISSOCK(stbuf->st_mode)) {
790 mode |= P9_STAT_MODE_SOCKET;
793 if (S_ISFIFO(stbuf->st_mode)) {
794 mode |= P9_STAT_MODE_NAMED_PIPE;
797 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
798 mode |= P9_STAT_MODE_DEVICE;
801 if (stbuf->st_mode & S_ISUID) {
802 mode |= P9_STAT_MODE_SETUID;
805 if (stbuf->st_mode & S_ISGID) {
806 mode |= P9_STAT_MODE_SETGID;
809 if (stbuf->st_mode & S_ISVTX) {
810 mode |= P9_STAT_MODE_SETVTX;
816 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
817 const struct stat *stbuf,
823 memset(v9stat, 0, sizeof(*v9stat));
825 stat_to_qid(stbuf, &v9stat->qid);
826 v9stat->mode = stat_to_v9mode(stbuf);
827 v9stat->atime = stbuf->st_atime;
828 v9stat->mtime = stbuf->st_mtime;
829 v9stat->length = stbuf->st_size;
831 v9fs_string_null(&v9stat->uid);
832 v9fs_string_null(&v9stat->gid);
833 v9fs_string_null(&v9stat->muid);
835 v9stat->n_uid = stbuf->st_uid;
836 v9stat->n_gid = stbuf->st_gid;
839 v9fs_string_null(&v9stat->extension);
841 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
842 err = v9fs_co_readlink(s, name, &v9stat->extension);
846 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
847 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
848 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
849 major(stbuf->st_rdev), minor(stbuf->st_rdev));
850 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
851 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
852 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
855 str = strrchr(name->data, '/');
862 v9fs_string_sprintf(&v9stat->name, "%s", str);
865 v9fs_string_size(&v9stat->name) +
866 v9fs_string_size(&v9stat->uid) +
867 v9fs_string_size(&v9stat->gid) +
868 v9fs_string_size(&v9stat->muid) +
869 v9fs_string_size(&v9stat->extension);
873 #define P9_STATS_MODE 0x00000001ULL
874 #define P9_STATS_NLINK 0x00000002ULL
875 #define P9_STATS_UID 0x00000004ULL
876 #define P9_STATS_GID 0x00000008ULL
877 #define P9_STATS_RDEV 0x00000010ULL
878 #define P9_STATS_ATIME 0x00000020ULL
879 #define P9_STATS_MTIME 0x00000040ULL
880 #define P9_STATS_CTIME 0x00000080ULL
881 #define P9_STATS_INO 0x00000100ULL
882 #define P9_STATS_SIZE 0x00000200ULL
883 #define P9_STATS_BLOCKS 0x00000400ULL
885 #define P9_STATS_BTIME 0x00000800ULL
886 #define P9_STATS_GEN 0x00001000ULL
887 #define P9_STATS_DATA_VERSION 0x00002000ULL
889 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
890 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
893 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
894 V9fsStatDotl *v9lstat)
896 memset(v9lstat, 0, sizeof(*v9lstat));
898 v9lstat->st_mode = stbuf->st_mode;
899 v9lstat->st_nlink = stbuf->st_nlink;
900 v9lstat->st_uid = stbuf->st_uid;
901 v9lstat->st_gid = stbuf->st_gid;
902 v9lstat->st_rdev = stbuf->st_rdev;
903 v9lstat->st_size = stbuf->st_size;
904 v9lstat->st_blksize = stbuf->st_blksize;
905 v9lstat->st_blocks = stbuf->st_blocks;
906 v9lstat->st_atime_sec = stbuf->st_atime;
907 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
908 v9lstat->st_mtime_sec = stbuf->st_mtime;
909 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
910 v9lstat->st_ctime_sec = stbuf->st_ctime;
911 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
912 /* Currently we only support BASIC fields in stat */
913 v9lstat->st_result_mask = P9_STATS_BASIC;
915 stat_to_qid(stbuf, &v9lstat->qid);
918 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
920 while (len && *iovcnt) {
921 if (len < sg->iov_len) {
935 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
940 for (i = 0; i < *cnt; i++) {
941 if ((total + sg[i].iov_len) > cap) {
942 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
946 total += sg[i].iov_len;
954 static void print_sg(struct iovec *sg, int cnt)
958 printf("sg[%d]: {", cnt);
959 for (i = 0; i < cnt; i++) {
963 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
968 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
971 v9fs_string_init(&str);
972 v9fs_string_copy(&str, dst);
973 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
974 v9fs_string_free(&str);
977 static void v9fs_version(void *opaque)
979 V9fsPDU *pdu = opaque;
980 V9fsState *s = pdu->s;
984 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
986 if (!strcmp(version.data, "9P2000.u")) {
987 s->proto_version = V9FS_PROTO_2000U;
988 } else if (!strcmp(version.data, "9P2000.L")) {
989 s->proto_version = V9FS_PROTO_2000L;
991 v9fs_string_sprintf(&version, "unknown");
994 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
995 complete_pdu(s, pdu, offset);
997 v9fs_string_free(&version);
1001 static void v9fs_attach(void *opaque)
1003 V9fsPDU *pdu = opaque;
1004 V9fsState *s = pdu->s;
1005 int32_t fid, afid, n_uname;
1006 V9fsString uname, aname;
1012 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1014 fidp = alloc_fid(s, fid);
1019 fidp->uid = n_uname;
1020 v9fs_string_sprintf(&fidp->path, "%s", "/");
1021 err = fid_to_qid(s, fidp, &qid);
1027 offset += pdu_marshal(pdu, offset, "Q", &qid);
1030 complete_pdu(s, pdu, err);
1031 v9fs_string_free(&uname);
1032 v9fs_string_free(&aname);
1035 static void v9fs_stat(void *opaque)
1043 V9fsPDU *pdu = opaque;
1044 V9fsState *s = pdu->s;
1046 pdu_unmarshal(pdu, offset, "d", &fid);
1047 fidp = lookup_fid(s, fid);
1052 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1056 err = stat_to_v9stat(s, &fidp->path, &stbuf, &v9stat);
1060 offset += pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1062 v9fs_stat_free(&v9stat);
1064 complete_pdu(s, pdu, err);
1067 static void v9fs_getattr(void *opaque)
1074 uint64_t request_mask;
1075 V9fsStatDotl v9stat_dotl;
1076 V9fsPDU *pdu = opaque;
1077 V9fsState *s = pdu->s;
1079 pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1081 fidp = lookup_fid(s, fid);
1087 * Currently we only support BASIC fields in stat, so there is no
1088 * need to look at request_mask.
1090 retval = v9fs_co_lstat(s, &fidp->path, &stbuf);
1094 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1096 retval += pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1098 complete_pdu(s, pdu, retval);
1101 /* From Linux kernel code */
1102 #define ATTR_MODE (1 << 0)
1103 #define ATTR_UID (1 << 1)
1104 #define ATTR_GID (1 << 2)
1105 #define ATTR_SIZE (1 << 3)
1106 #define ATTR_ATIME (1 << 4)
1107 #define ATTR_MTIME (1 << 5)
1108 #define ATTR_CTIME (1 << 6)
1109 #define ATTR_MASK 127
1110 #define ATTR_ATIME_SET (1 << 7)
1111 #define ATTR_MTIME_SET (1 << 8)
1113 static void v9fs_setattr(void *opaque)
1120 V9fsPDU *pdu = opaque;
1121 V9fsState *s = pdu->s;
1123 pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1125 fidp = lookup_fid(s, fid);
1130 if (v9iattr.valid & ATTR_MODE) {
1131 err = v9fs_co_chmod(s, &fidp->path, v9iattr.mode);
1136 if (v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1137 struct timespec times[2];
1138 if (v9iattr.valid & ATTR_ATIME) {
1139 if (v9iattr.valid & ATTR_ATIME_SET) {
1140 times[0].tv_sec = v9iattr.atime_sec;
1141 times[0].tv_nsec = v9iattr.atime_nsec;
1143 times[0].tv_nsec = UTIME_NOW;
1146 times[0].tv_nsec = UTIME_OMIT;
1148 if (v9iattr.valid & ATTR_MTIME) {
1149 if (v9iattr.valid & ATTR_MTIME_SET) {
1150 times[1].tv_sec = v9iattr.mtime_sec;
1151 times[1].tv_nsec = v9iattr.mtime_nsec;
1153 times[1].tv_nsec = UTIME_NOW;
1156 times[1].tv_nsec = UTIME_OMIT;
1158 err = v9fs_co_utimensat(s, &fidp->path, times);
1164 * If the only valid entry in iattr is ctime we can call
1165 * chown(-1,-1) to update the ctime of the file
1167 if ((v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1168 ((v9iattr.valid & ATTR_CTIME)
1169 && !((v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1170 if (!(v9iattr.valid & ATTR_UID)) {
1173 if (!(v9iattr.valid & ATTR_GID)) {
1176 err = v9fs_co_chown(s, &fidp->path, v9iattr.uid,
1182 if (v9iattr.valid & (ATTR_SIZE)) {
1183 err = v9fs_co_truncate(s, &fidp->path, v9iattr.size);
1190 complete_pdu(s, pdu, err);
1193 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1197 offset += pdu_marshal(pdu, offset, "w", nwnames);
1198 for (i = 0; i < nwnames; i++) {
1199 offset += pdu_marshal(pdu, offset, "Q", &qids[i]);
1204 static void v9fs_walk(void *opaque)
1207 V9fsQID *qids = NULL;
1213 int32_t fid, newfid;
1214 V9fsString *wnames = NULL;
1216 V9fsFidState *newfidp;
1217 V9fsPDU *pdu = opaque;
1218 V9fsState *s = pdu->s;
1220 offset += pdu_unmarshal(pdu, offset, "ddw", &fid,
1223 if (nwnames && nwnames <= P9_MAXWELEM) {
1224 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1225 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1226 for (i = 0; i < nwnames; i++) {
1227 offset += pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1230 } else if (nwnames > P9_MAXWELEM) {
1234 fidp = lookup_fid(s, fid);
1239 if (fid == newfid) {
1240 BUG_ON(fidp->fid_type != P9_FID_NONE);
1241 v9fs_string_init(&path);
1242 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1243 v9fs_string_sprintf(&path, "%s/%s",
1244 fidp->path.data, wnames[name_idx].data);
1245 v9fs_string_copy(&fidp->path, &path);
1247 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1249 v9fs_string_free(&path);
1252 stat_to_qid(&stbuf, &qids[name_idx]);
1254 v9fs_string_free(&path);
1256 newfidp = alloc_fid(s, newfid);
1257 if (newfidp == NULL) {
1261 newfidp->uid = fidp->uid;
1262 v9fs_string_init(&path);
1263 v9fs_string_copy(&newfidp->path, &fidp->path);
1264 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1265 v9fs_string_sprintf(&path, "%s/%s", newfidp->path.data,
1266 wnames[name_idx].data);
1267 v9fs_string_copy(&newfidp->path, &path);
1268 err = v9fs_co_lstat(s, &newfidp->path, &stbuf);
1270 free_fid(s, newfidp->fid);
1271 v9fs_string_free(&path);
1274 stat_to_qid(&stbuf, &qids[name_idx]);
1276 v9fs_string_free(&path);
1278 err = v9fs_walk_marshal(pdu, nwnames, qids);
1280 complete_pdu(s, pdu, err);
1281 if (nwnames && nwnames <= P9_MAXWELEM) {
1282 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1283 v9fs_string_free(&wnames[name_idx]);
1290 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1292 struct statfs stbuf;
1296 * iounit should be multiples of f_bsize (host filesystem block size
1297 * and as well as less than (client msize - P9_IOHDRSZ))
1299 if (!v9fs_co_statfs(s, name, &stbuf)) {
1300 iounit = stbuf.f_bsize;
1301 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1304 iounit = s->msize - P9_IOHDRSZ;
1309 static void v9fs_open(void *opaque)
1320 V9fsPDU *pdu = opaque;
1321 V9fsState *s = pdu->s;
1323 if (s->proto_version == V9FS_PROTO_2000L) {
1324 pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1326 pdu_unmarshal(pdu, offset, "db", &fid, &mode);
1328 fidp = lookup_fid(s, fid);
1333 BUG_ON(fidp->fid_type != P9_FID_NONE);
1335 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1339 stat_to_qid(&stbuf, &qid);
1340 if (S_ISDIR(stbuf.st_mode)) {
1341 err = v9fs_co_opendir(s, fidp);
1345 fidp->fid_type = P9_FID_DIR;
1346 offset += pdu_marshal(pdu, offset, "Qd", &qid, 0);
1349 if (s->proto_version == V9FS_PROTO_2000L) {
1351 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1352 /* Ignore direct disk access hint until the server supports it. */
1355 flags = omode_to_uflags(mode);
1357 err = v9fs_co_open(s, fidp, flags);
1361 fidp->fid_type = P9_FID_FILE;
1362 iounit = get_iounit(s, &fidp->path);
1363 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1367 complete_pdu(s, pdu, err);
1370 static void v9fs_lcreate(void *opaque)
1372 int32_t dfid, flags, mode;
1376 V9fsString fullname;
1382 V9fsPDU *pdu = opaque;
1384 v9fs_string_init(&fullname);
1385 pdu_unmarshal(pdu, offset, "dsddd", &dfid, &name, &flags,
1388 fidp = lookup_fid(pdu->s, dfid);
1393 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
1395 /* Ignore direct disk access hint until the server supports it. */
1398 err = v9fs_co_open2(pdu->s, fidp, fullname.data, gid, flags, mode);
1402 fidp->fid_type = P9_FID_FILE;
1403 iounit = get_iounit(pdu->s, &fullname);
1405 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
1407 fidp->fid_type = P9_FID_NONE;
1408 if (fidp->fs.fd > 0) {
1413 v9fs_string_copy(&fidp->path, &fullname);
1414 stat_to_qid(&stbuf, &qid);
1415 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1418 complete_pdu(pdu->s, pdu, err);
1419 v9fs_string_free(&name);
1420 v9fs_string_free(&fullname);
1423 static void v9fs_fsync(void *opaque)
1430 V9fsPDU *pdu = opaque;
1431 V9fsState *s = pdu->s;
1433 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1434 fidp = lookup_fid(s, fid);
1439 err = v9fs_co_fsync(s, fidp, datasync);
1444 complete_pdu(s, pdu, err);
1447 static void v9fs_clunk(void *opaque)
1452 V9fsPDU *pdu = opaque;
1453 V9fsState *s = pdu->s;
1455 pdu_unmarshal(pdu, offset, "d", &fid);
1456 err = free_fid(s, fid);
1462 complete_pdu(s, pdu, err);
1465 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu,
1466 V9fsFidState *fidp, int64_t off, int32_t max_count)
1472 xattr_len = fidp->fs.xattr.len;
1473 read_count = xattr_len - off;
1474 if (read_count > max_count) {
1475 read_count = max_count;
1476 } else if (read_count < 0) {
1478 * read beyond XATTR value
1482 offset += pdu_marshal(pdu, offset, "d", read_count);
1483 offset += pdu_pack(pdu, offset,
1484 ((char *)fidp->fs.xattr.value) + off,
1489 static int v9fs_do_readdir_with_stat(V9fsState *s, V9fsPDU *pdu,
1490 V9fsFidState *fidp, int32_t max_count)
1497 off_t saved_dir_pos;
1498 struct dirent *dent, *result;
1500 /* save the directory position */
1501 saved_dir_pos = v9fs_co_telldir(s, fidp);
1502 if (saved_dir_pos < 0) {
1503 return saved_dir_pos;
1506 dent = g_malloc(sizeof(struct dirent));
1509 v9fs_string_init(&name);
1510 err = v9fs_co_readdir_r(s, fidp, dent, &result);
1511 if (err || !result) {
1514 v9fs_string_sprintf(&name, "%s/%s", fidp->path.data, dent->d_name);
1515 err = v9fs_co_lstat(s, &name, &stbuf);
1519 err = stat_to_v9stat(s, &name, &stbuf, &v9stat);
1523 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1524 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1525 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1526 /* Ran out of buffer. Set dir back to old position and return */
1527 v9fs_co_seekdir(s, fidp, saved_dir_pos);
1528 v9fs_stat_free(&v9stat);
1529 v9fs_string_free(&name);
1534 v9fs_stat_free(&v9stat);
1535 v9fs_string_free(&name);
1536 saved_dir_pos = dent->d_off;
1540 v9fs_string_free(&name);
1547 static void v9fs_read(void *opaque)
1556 V9fsPDU *pdu = opaque;
1557 V9fsState *s = pdu->s;
1559 pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1560 fidp = lookup_fid(s, fid);
1565 if (fidp->fid_type == P9_FID_DIR) {
1568 v9fs_co_rewinddir(s, fidp);
1570 count = v9fs_do_readdir_with_stat(s, pdu, fidp, max_count);
1576 err += pdu_marshal(pdu, offset, "d", count);
1578 } else if (fidp->fid_type == P9_FID_FILE) {
1582 struct iovec iov[128]; /* FIXME: bad, bad, bad */
1585 pdu_marshal(pdu, offset + 4, "v", sg, &cnt);
1586 sg = cap_sg(sg, max_count, &cnt);
1591 /* Loop in case of EINTR */
1593 len = v9fs_co_preadv(s, fidp, sg, cnt, off);
1598 } while (len == -EINTR);
1600 /* IO error return the error */
1604 sg = adjust_sg(sg, len, &cnt);
1605 } while (count < max_count && len > 0);
1607 err += pdu_marshal(pdu, offset, "d", count);
1609 } else if (fidp->fid_type == P9_FID_XATTR) {
1610 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1615 complete_pdu(s, pdu, err);
1618 static size_t v9fs_readdir_data_size(V9fsString *name)
1621 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1622 * size of type (1) + size of name.size (2) + strlen(name.data)
1624 return 24 + v9fs_string_size(name);
1627 static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
1628 V9fsFidState *fidp, int32_t max_count)
1635 off_t saved_dir_pos;
1636 struct dirent *dent, *result;
1638 /* save the directory position */
1639 saved_dir_pos = v9fs_co_telldir(s, fidp);
1640 if (saved_dir_pos < 0) {
1641 return saved_dir_pos;
1644 dent = g_malloc(sizeof(struct dirent));
1647 err = v9fs_co_readdir_r(s, fidp, dent, &result);
1648 if (err || !result) {
1651 v9fs_string_init(&name);
1652 v9fs_string_sprintf(&name, "%s", dent->d_name);
1653 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1654 /* Ran out of buffer. Set dir back to old position and return */
1655 v9fs_co_seekdir(s, fidp, saved_dir_pos);
1656 v9fs_string_free(&name);
1661 * Fill up just the path field of qid because the client uses
1662 * only that. To fill the entire qid structure we will have
1663 * to stat each dirent found, which is expensive
1665 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1666 memcpy(&qid.path, &dent->d_ino, size);
1667 /* Fill the other fields with dummy values */
1671 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1672 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1674 dent->d_type, &name);
1676 v9fs_string_free(&name);
1677 saved_dir_pos = dent->d_off;
1686 static void v9fs_readdir(void *opaque)
1692 int64_t initial_offset;
1693 int32_t count, max_count;
1694 V9fsPDU *pdu = opaque;
1695 V9fsState *s = pdu->s;
1697 pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count);
1699 fidp = lookup_fid(s, fid);
1700 if (fidp == NULL || !fidp->fs.dir) {
1704 if (initial_offset == 0) {
1705 v9fs_co_rewinddir(s, fidp);
1707 v9fs_co_seekdir(s, fidp, initial_offset);
1709 count = v9fs_do_readdir(s, pdu, fidp, max_count);
1715 retval += pdu_marshal(pdu, offset, "d", count);
1718 complete_pdu(s, pdu, retval);
1721 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1722 int64_t off, int32_t count,
1723 struct iovec *sg, int cnt)
1732 xattr_len = fidp->fs.xattr.len;
1733 write_count = xattr_len - off;
1734 if (write_count > count) {
1735 write_count = count;
1736 } else if (write_count < 0) {
1738 * write beyond XATTR value len specified in
1744 offset += pdu_marshal(pdu, offset, "d", write_count);
1746 fidp->fs.xattr.copied_len += write_count;
1748 * Now copy the content from sg list
1750 for (i = 0; i < cnt; i++) {
1751 if (write_count > sg[i].iov_len) {
1752 to_copy = sg[i].iov_len;
1754 to_copy = write_count;
1756 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1757 /* updating vs->off since we are not using below */
1759 write_count -= to_copy;
1765 static void v9fs_write(void *opaque)
1776 struct iovec iov[128]; /* FIXME: bad, bad, bad */
1777 struct iovec *sg = iov;
1778 V9fsPDU *pdu = opaque;
1779 V9fsState *s = pdu->s;
1781 pdu_unmarshal(pdu, offset, "dqdv", &fid, &off, &count, sg, &cnt);
1782 fidp = lookup_fid(s, fid);
1787 if (fidp->fid_type == P9_FID_FILE) {
1788 if (fidp->fs.fd == -1) {
1792 } else if (fidp->fid_type == P9_FID_XATTR) {
1794 * setxattr operation
1796 err = v9fs_xattr_write(s, pdu, fidp, off, count, sg, cnt);
1802 sg = cap_sg(sg, count, &cnt);
1807 /* Loop in case of EINTR */
1809 len = v9fs_co_pwritev(s, fidp, sg, cnt, off);
1814 } while (len == -EINTR);
1816 /* IO error return the error */
1820 sg = adjust_sg(sg, len, &cnt);
1821 } while (total < count && len > 0);
1822 offset += pdu_marshal(pdu, offset, "d", total);
1825 complete_pdu(s, pdu, err);
1828 static void v9fs_create(void *opaque)
1839 V9fsString extension;
1840 V9fsString fullname;
1842 V9fsPDU *pdu = opaque;
1844 v9fs_string_init(&fullname);
1846 pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
1847 &perm, &mode, &extension);
1849 fidp = lookup_fid(pdu->s, fid);
1855 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
1856 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
1860 } else if (err != -ENOENT) {
1863 if (perm & P9_STAT_MODE_DIR) {
1864 err = v9fs_co_mkdir(pdu->s, fullname.data, perm & 0777,
1869 err = v9fs_co_opendir(pdu->s, fidp);
1873 fidp->fid_type = P9_FID_DIR;
1874 } else if (perm & P9_STAT_MODE_SYMLINK) {
1875 err = v9fs_co_symlink(pdu->s, fidp, extension.data,
1880 } else if (perm & P9_STAT_MODE_LINK) {
1881 int32_t nfid = atoi(extension.data);
1882 V9fsFidState *nfidp = lookup_fid(pdu->s, nfid);
1883 if (nfidp == NULL) {
1887 err = v9fs_co_link(pdu->s, &nfidp->path, &fullname);
1891 } else if (perm & P9_STAT_MODE_DEVICE) {
1893 uint32_t major, minor;
1896 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
1913 nmode |= perm & 0777;
1914 err = v9fs_co_mknod(pdu->s, &fullname, fidp->uid, -1,
1915 makedev(major, minor), nmode);
1919 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
1920 err = v9fs_co_mknod(pdu->s, &fullname, fidp->uid, -1,
1921 0, S_IFIFO | (perm & 0777));
1925 } else if (perm & P9_STAT_MODE_SOCKET) {
1926 err = v9fs_co_mknod(pdu->s, &fullname, fidp->uid, -1,
1927 0, S_IFSOCK | (perm & 0777));
1932 err = v9fs_co_open2(pdu->s, fidp, fullname.data, -1,
1933 omode_to_uflags(mode)|O_CREAT, perm);
1937 fidp->fid_type = P9_FID_FILE;
1939 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
1941 fidp->fid_type = P9_FID_NONE;
1947 iounit = get_iounit(pdu->s, &fidp->path);
1948 v9fs_string_copy(&fidp->path, &fullname);
1949 stat_to_qid(&stbuf, &qid);
1950 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1953 complete_pdu(pdu->s, pdu, err);
1954 v9fs_string_free(&name);
1955 v9fs_string_free(&extension);
1956 v9fs_string_free(&fullname);
1959 static void v9fs_symlink(void *opaque)
1961 V9fsPDU *pdu = opaque;
1964 V9fsString fullname;
1965 V9fsFidState *dfidp;
1973 v9fs_string_init(&fullname);
1974 pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
1976 dfidp = lookup_fid(pdu->s, dfid);
1977 if (dfidp == NULL) {
1982 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
1983 err = v9fs_co_symlink(pdu->s, dfidp, symname.data, fullname.data, gid);
1987 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
1991 stat_to_qid(&stbuf, &qid);
1992 offset += pdu_marshal(pdu, offset, "Q", &qid);
1995 complete_pdu(pdu->s, pdu, err);
1996 v9fs_string_free(&name);
1997 v9fs_string_free(&symname);
1998 v9fs_string_free(&fullname);
2001 static void v9fs_flush(void *opaque)
2003 V9fsPDU *pdu = opaque;
2004 V9fsState *s = pdu->s;
2005 /* A nop call with no return */
2006 complete_pdu(s, pdu, 7);
2010 static void v9fs_link(void *opaque)
2012 V9fsPDU *pdu = opaque;
2013 V9fsState *s = pdu->s;
2014 int32_t dfid, oldfid;
2015 V9fsFidState *dfidp, *oldfidp;
2016 V9fsString name, fullname;
2020 v9fs_string_init(&fullname);
2022 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2024 dfidp = lookup_fid(s, dfid);
2025 if (dfidp == NULL) {
2030 oldfidp = lookup_fid(s, oldfid);
2031 if (oldfidp == NULL) {
2036 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2037 err = v9fs_co_link(s, &oldfidp->path, &fullname);
2041 v9fs_string_free(&fullname);
2044 v9fs_string_free(&name);
2045 complete_pdu(s, pdu, err);
2048 static void v9fs_remove(void *opaque)
2054 V9fsPDU *pdu = opaque;
2056 pdu_unmarshal(pdu, offset, "d", &fid);
2058 fidp = lookup_fid(pdu->s, fid);
2063 err = v9fs_co_remove(pdu->s, &fidp->path);
2068 /* For TREMOVE we need to clunk the fid even on failed remove */
2069 free_fid(pdu->s, fidp->fid);
2071 complete_pdu(pdu->s, pdu, err);
2074 static int v9fs_complete_rename(V9fsState *s, V9fsFidState *fidp,
2075 int32_t newdirfid, V9fsString *name)
2079 char *old_name, *new_name;
2081 if (newdirfid != -1) {
2082 V9fsFidState *dirfidp;
2083 dirfidp = lookup_fid(s, newdirfid);
2084 if (dirfidp == NULL) {
2088 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2090 new_name = g_malloc0(dirfidp->path.size + name->size + 2);
2092 strcpy(new_name, dirfidp->path.data);
2093 strcat(new_name, "/");
2094 strcat(new_name + dirfidp->path.size, name->data);
2096 old_name = fidp->path.data;
2097 end = strrchr(old_name, '/');
2103 new_name = g_malloc0(end - old_name + name->size + 1);
2105 strncat(new_name, old_name, end - old_name);
2106 strncat(new_name + (end - old_name), name->data, name->size);
2109 v9fs_string_free(name);
2110 name->data = new_name;
2111 name->size = strlen(new_name);
2113 if (strcmp(new_name, fidp->path.data) != 0) {
2114 err = v9fs_co_rename(s, &fidp->path, name);
2118 V9fsFidState *tfidp;
2120 * Fixup fid's pointing to the old name to
2121 * start pointing to the new name
2123 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2124 if (fidp == tfidp) {
2126 * we replace name of this fid towards the end
2127 * so that our below strcmp will work
2131 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2132 /* replace the name */
2133 v9fs_fix_path(&tfidp->path, name, strlen(fidp->path.data));
2136 v9fs_string_copy(&fidp->path, name);
2142 static void v9fs_rename(void *opaque)
2150 V9fsPDU *pdu = opaque;
2151 V9fsState *s = pdu->s;
2153 pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2155 fidp = lookup_fid(s, fid);
2160 BUG_ON(fidp->fid_type != P9_FID_NONE);
2162 err = v9fs_complete_rename(s, fidp, newdirfid, &name);
2167 complete_pdu(s, pdu, err);
2168 v9fs_string_free(&name);
2171 static void v9fs_wstat(void *opaque)
2180 V9fsPDU *pdu = opaque;
2181 V9fsState *s = pdu->s;
2183 pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2184 fidp = lookup_fid(s, fid);
2189 /* do we need to sync the file? */
2190 if (donttouch_stat(&v9stat)) {
2191 err = v9fs_co_fsync(s, fidp, 0);
2194 if (v9stat.mode != -1) {
2196 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
2200 v9_mode = stat_to_v9mode(&stbuf);
2201 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2202 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2203 /* Attempting to change the type */
2207 err = v9fs_co_chmod(s, &fidp->path,
2208 v9mode_to_mode(v9stat.mode,
2209 &v9stat.extension));
2214 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2215 struct timespec times[2];
2216 if (v9stat.atime != -1) {
2217 times[0].tv_sec = v9stat.atime;
2218 times[0].tv_nsec = 0;
2220 times[0].tv_nsec = UTIME_OMIT;
2222 if (v9stat.mtime != -1) {
2223 times[1].tv_sec = v9stat.mtime;
2224 times[1].tv_nsec = 0;
2226 times[1].tv_nsec = UTIME_OMIT;
2228 err = v9fs_co_utimensat(s, &fidp->path, times);
2233 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2234 err = v9fs_co_chown(s, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2239 if (v9stat.name.size != 0) {
2240 err = v9fs_complete_rename(s, fidp, -1, &v9stat.name);
2245 if (v9stat.length != -1) {
2246 err = v9fs_co_truncate(s, &fidp->path, v9stat.length);
2253 v9fs_stat_free(&v9stat);
2254 complete_pdu(s, pdu, err);
2257 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2269 int32_t bsize_factor;
2272 * compute bsize factor based on host file system block size
2275 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2276 if (!bsize_factor) {
2279 f_type = stbuf->f_type;
2280 f_bsize = stbuf->f_bsize;
2281 f_bsize *= bsize_factor;
2283 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2284 * adjust(divide) the number of blocks, free blocks and available
2285 * blocks by bsize factor
2287 f_blocks = stbuf->f_blocks/bsize_factor;
2288 f_bfree = stbuf->f_bfree/bsize_factor;
2289 f_bavail = stbuf->f_bavail/bsize_factor;
2290 f_files = stbuf->f_files;
2291 f_ffree = stbuf->f_ffree;
2292 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2293 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2294 f_namelen = stbuf->f_namelen;
2296 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2297 f_type, f_bsize, f_blocks, f_bfree,
2298 f_bavail, f_files, f_ffree,
2299 fsid_val, f_namelen);
2302 static void v9fs_statfs(void *opaque)
2308 struct statfs stbuf;
2309 V9fsPDU *pdu = opaque;
2310 V9fsState *s = pdu->s;
2312 pdu_unmarshal(pdu, offset, "d", &fid);
2313 fidp = lookup_fid(s, fid);
2318 retval = v9fs_co_statfs(s, &fidp->path, &stbuf);
2323 retval += v9fs_fill_statfs(s, pdu, &stbuf);
2325 complete_pdu(s, pdu, retval);
2329 static void v9fs_mknod(void *opaque)
2341 V9fsString fullname;
2343 V9fsPDU *pdu = opaque;
2344 V9fsState *s = pdu->s;
2346 v9fs_string_init(&fullname);
2347 pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2348 &major, &minor, &gid);
2350 fidp = lookup_fid(s, fid);
2355 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2356 err = v9fs_co_mknod(s, &fullname, fidp->uid, gid,
2357 makedev(major, minor), mode);
2361 err = v9fs_co_lstat(s, &fullname, &stbuf);
2365 stat_to_qid(&stbuf, &qid);
2367 err += pdu_marshal(pdu, offset, "Q", &qid);
2369 complete_pdu(s, pdu, err);
2370 v9fs_string_free(&fullname);
2371 v9fs_string_free(&name);
2375 * Implement posix byte range locking code
2376 * Server side handling of locking code is very simple, because 9p server in
2377 * QEMU can handle only one client. And most of the lock handling
2378 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2379 * do any thing in * qemu 9p server side lock code path.
2380 * So when a TLOCK request comes, always return success
2382 static void v9fs_lock(void *opaque)
2389 int32_t fid, err = 0;
2390 V9fsPDU *pdu = opaque;
2391 V9fsState *s = pdu->s;
2393 flock = g_malloc(sizeof(*flock));
2394 pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock->type,
2395 &flock->flags, &flock->start, &flock->length,
2396 &flock->proc_id, &flock->client_id);
2397 status = P9_LOCK_ERROR;
2399 /* We support only block flag now (that too ignored currently) */
2400 if (flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
2404 fidp = lookup_fid(s, fid);
2409 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2413 status = P9_LOCK_SUCCESS;
2416 err += pdu_marshal(pdu, offset, "b", status);
2417 complete_pdu(s, pdu, err);
2422 * When a TGETLOCK request comes, always return success because all lock
2423 * handling is done by client's VFS layer.
2425 static void v9fs_getlock(void *opaque)
2431 int32_t fid, err = 0;
2432 V9fsPDU *pdu = opaque;
2433 V9fsState *s = pdu->s;
2435 glock = g_malloc(sizeof(*glock));
2436 pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock->type,
2437 &glock->start, &glock->length, &glock->proc_id,
2440 fidp = lookup_fid(s, fid);
2445 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2449 glock->type = F_UNLCK;
2450 offset += pdu_marshal(pdu, offset, "bqqds", glock->type,
2451 glock->start, glock->length, glock->proc_id,
2455 complete_pdu(s, pdu, err);
2459 static void v9fs_mkdir(void *opaque)
2461 V9fsPDU *pdu = opaque;
2465 V9fsString name, fullname;
2472 v9fs_string_init(&fullname);
2473 pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2475 fidp = lookup_fid(pdu->s, fid);
2480 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2481 err = v9fs_co_mkdir(pdu->s, fullname.data, mode, fidp->uid, gid);
2485 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
2489 stat_to_qid(&stbuf, &qid);
2490 offset += pdu_marshal(pdu, offset, "Q", &qid);
2493 complete_pdu(pdu->s, pdu, err);
2494 v9fs_string_free(&fullname);
2495 v9fs_string_free(&name);
2498 static void v9fs_xattrwalk(void *opaque)
2504 int32_t fid, newfid;
2505 V9fsFidState *file_fidp;
2506 V9fsFidState *xattr_fidp;
2507 V9fsPDU *pdu = opaque;
2508 V9fsState *s = pdu->s;
2510 pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2511 file_fidp = lookup_fid(s, fid);
2512 if (file_fidp == NULL) {
2516 xattr_fidp = alloc_fid(s, newfid);
2517 if (xattr_fidp == NULL) {
2521 v9fs_string_copy(&xattr_fidp->path, &file_fidp->path);
2522 if (name.data[0] == 0) {
2524 * listxattr request. Get the size first
2526 size = v9fs_co_llistxattr(s, &xattr_fidp->path, NULL, 0);
2529 free_fid(s, xattr_fidp->fid);
2533 * Read the xattr value
2535 xattr_fidp->fs.xattr.len = size;
2536 xattr_fidp->fid_type = P9_FID_XATTR;
2537 xattr_fidp->fs.xattr.copied_len = -1;
2539 xattr_fidp->fs.xattr.value = g_malloc(size);
2540 err = v9fs_co_llistxattr(s, &xattr_fidp->path,
2541 xattr_fidp->fs.xattr.value,
2542 xattr_fidp->fs.xattr.len);
2544 free_fid(s, xattr_fidp->fid);
2548 offset += pdu_marshal(pdu, offset, "q", size);
2552 * specific xattr fid. We check for xattr
2553 * presence also collect the xattr size
2555 size = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2559 free_fid(s, xattr_fidp->fid);
2563 * Read the xattr value
2565 xattr_fidp->fs.xattr.len = size;
2566 xattr_fidp->fid_type = P9_FID_XATTR;
2567 xattr_fidp->fs.xattr.copied_len = -1;
2569 xattr_fidp->fs.xattr.value = g_malloc(size);
2570 err = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2571 &name, xattr_fidp->fs.xattr.value,
2572 xattr_fidp->fs.xattr.len);
2574 free_fid(s, xattr_fidp->fid);
2578 offset += pdu_marshal(pdu, offset, "q", size);
2582 complete_pdu(s, pdu, err);
2583 v9fs_string_free(&name);
2586 static void v9fs_xattrcreate(void *opaque)
2594 V9fsFidState *file_fidp;
2595 V9fsFidState *xattr_fidp;
2596 V9fsPDU *pdu = opaque;
2597 V9fsState *s = pdu->s;
2599 pdu_unmarshal(pdu, offset, "dsqd",
2600 &fid, &name, &size, &flags);
2602 file_fidp = lookup_fid(s, fid);
2603 if (file_fidp == NULL) {
2607 /* Make the file fid point to xattr */
2608 xattr_fidp = file_fidp;
2609 xattr_fidp->fid_type = P9_FID_XATTR;
2610 xattr_fidp->fs.xattr.copied_len = 0;
2611 xattr_fidp->fs.xattr.len = size;
2612 xattr_fidp->fs.xattr.flags = flags;
2613 v9fs_string_init(&xattr_fidp->fs.xattr.name);
2614 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
2616 xattr_fidp->fs.xattr.value = g_malloc(size);
2618 xattr_fidp->fs.xattr.value = NULL;
2622 complete_pdu(s, pdu, err);
2623 v9fs_string_free(&name);
2626 static void v9fs_readlink(void *opaque)
2628 V9fsPDU *pdu = opaque;
2635 pdu_unmarshal(pdu, offset, "d", &fid);
2636 fidp = lookup_fid(pdu->s, fid);
2642 v9fs_string_init(&target);
2643 err = v9fs_co_readlink(pdu->s, &fidp->path, &target);
2647 offset += pdu_marshal(pdu, offset, "s", &target);
2649 v9fs_string_free(&target);
2651 complete_pdu(pdu->s, pdu, err);
2654 static CoroutineEntry *pdu_co_handlers[] = {
2655 [P9_TREADDIR] = v9fs_readdir,
2656 [P9_TSTATFS] = v9fs_statfs,
2657 [P9_TGETATTR] = v9fs_getattr,
2658 [P9_TSETATTR] = v9fs_setattr,
2659 [P9_TXATTRWALK] = v9fs_xattrwalk,
2660 [P9_TXATTRCREATE] = v9fs_xattrcreate,
2661 [P9_TMKNOD] = v9fs_mknod,
2662 [P9_TRENAME] = v9fs_rename,
2663 [P9_TLOCK] = v9fs_lock,
2664 [P9_TGETLOCK] = v9fs_getlock,
2665 [P9_TREADLINK] = v9fs_readlink,
2666 [P9_TMKDIR] = v9fs_mkdir,
2667 [P9_TVERSION] = v9fs_version,
2668 [P9_TLOPEN] = v9fs_open,
2669 [P9_TATTACH] = v9fs_attach,
2670 [P9_TSTAT] = v9fs_stat,
2671 [P9_TWALK] = v9fs_walk,
2672 [P9_TCLUNK] = v9fs_clunk,
2673 [P9_TFSYNC] = v9fs_fsync,
2674 [P9_TOPEN] = v9fs_open,
2675 [P9_TREAD] = v9fs_read,
2677 [P9_TAUTH] = v9fs_auth,
2679 [P9_TFLUSH] = v9fs_flush,
2680 [P9_TLINK] = v9fs_link,
2681 [P9_TSYMLINK] = v9fs_symlink,
2682 [P9_TCREATE] = v9fs_create,
2683 [P9_TLCREATE] = v9fs_lcreate,
2684 [P9_TWRITE] = v9fs_write,
2685 [P9_TWSTAT] = v9fs_wstat,
2686 [P9_TREMOVE] = v9fs_remove,
2689 static void v9fs_op_not_supp(void *opaque)
2691 V9fsPDU *pdu = opaque;
2692 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
2695 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2698 CoroutineEntry *handler;
2703 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
2704 (pdu_co_handlers[pdu->id] == NULL)) {
2705 handler = v9fs_op_not_supp;
2707 handler = pdu_co_handlers[pdu->id];
2709 co = qemu_coroutine_create(handler);
2710 qemu_coroutine_enter(co, pdu);
2713 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2715 V9fsState *s = (V9fsState *)vdev;
2719 while ((pdu = alloc_pdu(s)) &&
2720 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2723 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2724 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2726 ptr = pdu->elem.out_sg[0].iov_base;
2728 memcpy(&pdu->size, ptr, 4);
2730 memcpy(&pdu->tag, ptr + 5, 2);