Merge remote branch 'kwolf/for-anthony' into staging
[qemu.git] / hw / virtio-9p.c
blob32fa3bcc5c8b1d9e78db626e2836bca9ea5bad4c
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
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 "virtio.h"
15 #include "pc.h"
16 #include "qemu_socket.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-debug.h"
21 int debug_9p_pdu;
23 enum {
24 Oread = 0x00,
25 Owrite = 0x01,
26 Ordwr = 0x02,
27 Oexec = 0x03,
28 Oexcl = 0x04,
29 Otrunc = 0x10,
30 Orexec = 0x20,
31 Orclose = 0x40,
32 Oappend = 0x80,
35 static int omode_to_uflags(int8_t mode)
37 int ret = 0;
39 switch (mode & 3) {
40 case Oread:
41 ret = O_RDONLY;
42 break;
43 case Ordwr:
44 ret = O_RDWR;
45 break;
46 case Owrite:
47 ret = O_WRONLY;
48 break;
49 case Oexec:
50 ret = O_RDONLY;
51 break;
54 if (mode & Otrunc) {
55 ret |= O_TRUNC;
58 if (mode & Oappend) {
59 ret |= O_APPEND;
62 if (mode & Oexcl) {
63 ret |= O_EXCL;
66 return ret;
69 void cred_init(FsCred *credp)
71 credp->fc_uid = -1;
72 credp->fc_gid = -1;
73 credp->fc_mode = -1;
74 credp->fc_rdev = -1;
77 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
79 return s->ops->lstat(&s->ctx, path->data, stbuf);
82 static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
84 ssize_t len;
86 buf->data = qemu_malloc(1024);
88 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
89 if (len > -1) {
90 buf->size = len;
91 buf->data[len] = 0;
94 return len;
97 static int v9fs_do_close(V9fsState *s, int fd)
99 return s->ops->close(&s->ctx, fd);
102 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
104 return s->ops->closedir(&s->ctx, dir);
107 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
109 return s->ops->open(&s->ctx, path->data, flags);
112 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
114 return s->ops->opendir(&s->ctx, path->data);
117 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
119 return s->ops->rewinddir(&s->ctx, dir);
122 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
124 return s->ops->telldir(&s->ctx, dir);
127 static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
129 return s->ops->readdir(&s->ctx, dir);
132 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
134 return s->ops->seekdir(&s->ctx, dir, off);
137 static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
138 int iovcnt)
140 return s->ops->readv(&s->ctx, fd, iov, iovcnt);
143 static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
145 return s->ops->lseek(&s->ctx, fd, offset, whence);
148 static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
149 int iovcnt)
151 return s->ops->writev(&s->ctx, fd, iov, iovcnt);
154 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
156 FsCred cred;
157 cred_init(&cred);
158 cred.fc_mode = mode;
159 return s->ops->chmod(&s->ctx, path->data, &cred);
162 static int v9fs_do_mknod(V9fsState *s, char *name,
163 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
165 FsCred cred;
166 cred_init(&cred);
167 cred.fc_uid = uid;
168 cred.fc_gid = gid;
169 cred.fc_mode = mode;
170 cred.fc_rdev = dev;
171 return s->ops->mknod(&s->ctx, name, &cred);
174 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
175 uid_t uid, gid_t gid)
177 FsCred cred;
179 cred_init(&cred);
180 cred.fc_uid = uid;
181 cred.fc_gid = gid;
182 cred.fc_mode = mode;
184 return s->ops->mkdir(&s->ctx, name, &cred);
187 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
189 return s->ops->fstat(&s->ctx, fd, stbuf);
192 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
193 int flags, int mode)
195 FsCred cred;
197 cred_init(&cred);
198 cred.fc_uid = uid;
199 cred.fc_gid = gid;
200 cred.fc_mode = mode & 07777;
201 flags = flags;
203 return s->ops->open2(&s->ctx, fullname, flags, &cred);
206 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
207 const char *oldpath, const char *newpath, gid_t gid)
209 FsCred cred;
210 cred_init(&cred);
211 cred.fc_uid = fidp->uid;
212 cred.fc_gid = gid;
213 cred.fc_mode = 0777;
215 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
218 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
220 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
223 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
225 return s->ops->truncate(&s->ctx, path->data, size);
228 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
229 V9fsString *newpath)
231 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
234 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
236 FsCred cred;
237 cred_init(&cred);
238 cred.fc_uid = uid;
239 cred.fc_gid = gid;
241 return s->ops->chown(&s->ctx, path->data, &cred);
244 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
245 const struct timespec times[2])
247 return s->ops->utimensat(&s->ctx, path->data, times);
250 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
252 return s->ops->remove(&s->ctx, path->data);
255 static int v9fs_do_fsync(V9fsState *s, int fd)
257 return s->ops->fsync(&s->ctx, fd);
260 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
262 return s->ops->statfs(&s->ctx, path->data, stbuf);
265 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
266 V9fsString *xattr_name,
267 void *value, size_t size)
269 return s->ops->lgetxattr(&s->ctx, path->data,
270 xattr_name->data, value, size);
273 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
274 void *value, size_t size)
276 return s->ops->llistxattr(&s->ctx, path->data,
277 value, size);
280 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
281 V9fsString *xattr_name,
282 void *value, size_t size, int flags)
284 return s->ops->lsetxattr(&s->ctx, path->data,
285 xattr_name->data, value, size, flags);
288 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
289 V9fsString *xattr_name)
291 return s->ops->lremovexattr(&s->ctx, path->data,
292 xattr_name->data);
296 static void v9fs_string_init(V9fsString *str)
298 str->data = NULL;
299 str->size = 0;
302 static void v9fs_string_free(V9fsString *str)
304 qemu_free(str->data);
305 str->data = NULL;
306 str->size = 0;
309 static void v9fs_string_null(V9fsString *str)
311 v9fs_string_free(str);
314 static int number_to_string(void *arg, char type)
316 unsigned int ret = 0;
318 switch (type) {
319 case 'u': {
320 unsigned int num = *(unsigned int *)arg;
322 do {
323 ret++;
324 num = num/10;
325 } while (num);
326 break;
328 default:
329 printf("Number_to_string: Unknown number format\n");
330 return -1;
333 return ret;
336 static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
338 va_list ap2;
339 char *iter = (char *)fmt;
340 int len = 0;
341 int nr_args = 0;
342 char *arg_char_ptr;
343 unsigned int arg_uint;
345 /* Find the number of %'s that denotes an argument */
346 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
347 nr_args++;
348 iter++;
351 len = strlen(fmt) - 2*nr_args;
353 if (!nr_args) {
354 goto alloc_print;
357 va_copy(ap2, ap);
359 iter = (char *)fmt;
361 /* Now parse the format string */
362 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
363 iter++;
364 switch (*iter) {
365 case 'u':
366 arg_uint = va_arg(ap2, unsigned int);
367 len += number_to_string((void *)&arg_uint, 'u');
368 break;
369 case 's':
370 arg_char_ptr = va_arg(ap2, char *);
371 len += strlen(arg_char_ptr);
372 break;
373 case 'c':
374 len += 1;
375 break;
376 default:
377 fprintf(stderr,
378 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
379 return -1;
381 iter++;
384 alloc_print:
385 *strp = qemu_malloc((len + 1) * sizeof(**strp));
387 return vsprintf(*strp, fmt, ap);
390 static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
392 va_list ap;
393 int err;
395 v9fs_string_free(str);
397 va_start(ap, fmt);
398 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
399 BUG_ON(err == -1);
400 va_end(ap);
402 str->size = err;
405 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
407 v9fs_string_free(lhs);
408 v9fs_string_sprintf(lhs, "%s", rhs->data);
411 static size_t v9fs_string_size(V9fsString *str)
413 return str->size;
416 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
418 V9fsFidState *f;
420 for (f = s->fid_list; f; f = f->next) {
421 if (f->fid == fid) {
422 return f;
426 return NULL;
429 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
431 V9fsFidState *f;
433 f = lookup_fid(s, fid);
434 if (f) {
435 return NULL;
438 f = qemu_mallocz(sizeof(V9fsFidState));
440 f->fid = fid;
441 f->fid_type = P9_FID_NONE;
443 f->next = s->fid_list;
444 s->fid_list = f;
446 return f;
449 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
451 int retval = 0;
453 if (fidp->fs.xattr.copied_len == -1) {
454 /* getxattr/listxattr fid */
455 goto free_value;
458 * if this is fid for setxattr. clunk should
459 * result in setxattr localcall
461 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
462 /* clunk after partial write */
463 retval = -EINVAL;
464 goto free_out;
466 if (fidp->fs.xattr.len) {
467 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
468 fidp->fs.xattr.value,
469 fidp->fs.xattr.len,
470 fidp->fs.xattr.flags);
471 } else {
472 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
474 free_out:
475 v9fs_string_free(&fidp->fs.xattr.name);
476 free_value:
477 if (fidp->fs.xattr.value) {
478 qemu_free(fidp->fs.xattr.value);
480 return retval;
483 static int free_fid(V9fsState *s, int32_t fid)
485 int retval = 0;
486 V9fsFidState **fidpp, *fidp;
488 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
489 if ((*fidpp)->fid == fid) {
490 break;
494 if (*fidpp == NULL) {
495 return -ENOENT;
498 fidp = *fidpp;
499 *fidpp = fidp->next;
501 if (fidp->fid_type == P9_FID_FILE) {
502 v9fs_do_close(s, fidp->fs.fd);
503 } else if (fidp->fid_type == P9_FID_DIR) {
504 v9fs_do_closedir(s, fidp->fs.dir);
505 } else if (fidp->fid_type == P9_FID_XATTR) {
506 retval = v9fs_xattr_fid_clunk(s, fidp);
508 v9fs_string_free(&fidp->path);
509 qemu_free(fidp);
511 return retval;
514 #define P9_QID_TYPE_DIR 0x80
515 #define P9_QID_TYPE_SYMLINK 0x02
517 #define P9_STAT_MODE_DIR 0x80000000
518 #define P9_STAT_MODE_APPEND 0x40000000
519 #define P9_STAT_MODE_EXCL 0x20000000
520 #define P9_STAT_MODE_MOUNT 0x10000000
521 #define P9_STAT_MODE_AUTH 0x08000000
522 #define P9_STAT_MODE_TMP 0x04000000
523 #define P9_STAT_MODE_SYMLINK 0x02000000
524 #define P9_STAT_MODE_LINK 0x01000000
525 #define P9_STAT_MODE_DEVICE 0x00800000
526 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
527 #define P9_STAT_MODE_SOCKET 0x00100000
528 #define P9_STAT_MODE_SETUID 0x00080000
529 #define P9_STAT_MODE_SETGID 0x00040000
530 #define P9_STAT_MODE_SETVTX 0x00010000
532 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
533 P9_STAT_MODE_SYMLINK | \
534 P9_STAT_MODE_LINK | \
535 P9_STAT_MODE_DEVICE | \
536 P9_STAT_MODE_NAMED_PIPE | \
537 P9_STAT_MODE_SOCKET)
539 /* This is the algorithm from ufs in spfs */
540 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
542 size_t size;
544 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
545 memcpy(&qidp->path, &stbuf->st_ino, size);
546 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
547 qidp->type = 0;
548 if (S_ISDIR(stbuf->st_mode)) {
549 qidp->type |= P9_QID_TYPE_DIR;
551 if (S_ISLNK(stbuf->st_mode)) {
552 qidp->type |= P9_QID_TYPE_SYMLINK;
556 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
558 struct stat stbuf;
559 int err;
561 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
562 if (err) {
563 return err;
566 stat_to_qid(&stbuf, qidp);
567 return 0;
570 static V9fsPDU *alloc_pdu(V9fsState *s)
572 V9fsPDU *pdu = NULL;
574 if (!QLIST_EMPTY(&s->free_list)) {
575 pdu = QLIST_FIRST(&s->free_list);
576 QLIST_REMOVE(pdu, next);
578 return pdu;
581 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
583 if (pdu) {
584 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
588 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
589 size_t offset, size_t size, int pack)
591 int i = 0;
592 size_t copied = 0;
594 for (i = 0; size && i < sg_count; i++) {
595 size_t len;
596 if (offset >= sg[i].iov_len) {
597 /* skip this sg */
598 offset -= sg[i].iov_len;
599 continue;
600 } else {
601 len = MIN(sg[i].iov_len - offset, size);
602 if (pack) {
603 memcpy(sg[i].iov_base + offset, addr, len);
604 } else {
605 memcpy(addr, sg[i].iov_base + offset, len);
607 size -= len;
608 copied += len;
609 addr += len;
610 if (size) {
611 offset = 0;
612 continue;
617 return copied;
620 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
622 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
623 offset, size, 0);
626 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
627 size_t size)
629 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
630 offset, size, 1);
633 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
635 size_t pos = 0;
636 int i, j;
637 struct iovec *src_sg;
638 unsigned int num;
640 if (rx) {
641 src_sg = pdu->elem.in_sg;
642 num = pdu->elem.in_num;
643 } else {
644 src_sg = pdu->elem.out_sg;
645 num = pdu->elem.out_num;
648 j = 0;
649 for (i = 0; i < num; i++) {
650 if (offset <= pos) {
651 sg[j].iov_base = src_sg[i].iov_base;
652 sg[j].iov_len = src_sg[i].iov_len;
653 j++;
654 } else if (offset < (src_sg[i].iov_len + pos)) {
655 sg[j].iov_base = src_sg[i].iov_base;
656 sg[j].iov_len = src_sg[i].iov_len;
657 sg[j].iov_base += (offset - pos);
658 sg[j].iov_len -= (offset - pos);
659 j++;
661 pos += src_sg[i].iov_len;
664 return j;
667 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
669 size_t old_offset = offset;
670 va_list ap;
671 int i;
673 va_start(ap, fmt);
674 for (i = 0; fmt[i]; i++) {
675 switch (fmt[i]) {
676 case 'b': {
677 uint8_t *valp = va_arg(ap, uint8_t *);
678 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
679 break;
681 case 'w': {
682 uint16_t val, *valp;
683 valp = va_arg(ap, uint16_t *);
684 val = le16_to_cpupu(valp);
685 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
686 *valp = val;
687 break;
689 case 'd': {
690 uint32_t val, *valp;
691 valp = va_arg(ap, uint32_t *);
692 val = le32_to_cpupu(valp);
693 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
694 *valp = val;
695 break;
697 case 'q': {
698 uint64_t val, *valp;
699 valp = va_arg(ap, uint64_t *);
700 val = le64_to_cpup(valp);
701 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
702 *valp = val;
703 break;
705 case 'v': {
706 struct iovec *iov = va_arg(ap, struct iovec *);
707 int *iovcnt = va_arg(ap, int *);
708 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
709 break;
711 case 's': {
712 V9fsString *str = va_arg(ap, V9fsString *);
713 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
714 /* FIXME: sanity check str->size */
715 str->data = qemu_malloc(str->size + 1);
716 offset += pdu_unpack(str->data, pdu, offset, str->size);
717 str->data[str->size] = 0;
718 break;
720 case 'Q': {
721 V9fsQID *qidp = va_arg(ap, V9fsQID *);
722 offset += pdu_unmarshal(pdu, offset, "bdq",
723 &qidp->type, &qidp->version, &qidp->path);
724 break;
726 case 'S': {
727 V9fsStat *statp = va_arg(ap, V9fsStat *);
728 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
729 &statp->size, &statp->type, &statp->dev,
730 &statp->qid, &statp->mode, &statp->atime,
731 &statp->mtime, &statp->length,
732 &statp->name, &statp->uid, &statp->gid,
733 &statp->muid, &statp->extension,
734 &statp->n_uid, &statp->n_gid,
735 &statp->n_muid);
736 break;
738 case 'I': {
739 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
740 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
741 &iattr->valid, &iattr->mode,
742 &iattr->uid, &iattr->gid, &iattr->size,
743 &iattr->atime_sec, &iattr->atime_nsec,
744 &iattr->mtime_sec, &iattr->mtime_nsec);
745 break;
747 default:
748 break;
752 va_end(ap);
754 return offset - old_offset;
757 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
759 size_t old_offset = offset;
760 va_list ap;
761 int i;
763 va_start(ap, fmt);
764 for (i = 0; fmt[i]; i++) {
765 switch (fmt[i]) {
766 case 'b': {
767 uint8_t val = va_arg(ap, int);
768 offset += pdu_pack(pdu, offset, &val, sizeof(val));
769 break;
771 case 'w': {
772 uint16_t val;
773 cpu_to_le16w(&val, va_arg(ap, int));
774 offset += pdu_pack(pdu, offset, &val, sizeof(val));
775 break;
777 case 'd': {
778 uint32_t val;
779 cpu_to_le32w(&val, va_arg(ap, uint32_t));
780 offset += pdu_pack(pdu, offset, &val, sizeof(val));
781 break;
783 case 'q': {
784 uint64_t val;
785 cpu_to_le64w(&val, va_arg(ap, uint64_t));
786 offset += pdu_pack(pdu, offset, &val, sizeof(val));
787 break;
789 case 'v': {
790 struct iovec *iov = va_arg(ap, struct iovec *);
791 int *iovcnt = va_arg(ap, int *);
792 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
793 break;
795 case 's': {
796 V9fsString *str = va_arg(ap, V9fsString *);
797 offset += pdu_marshal(pdu, offset, "w", str->size);
798 offset += pdu_pack(pdu, offset, str->data, str->size);
799 break;
801 case 'Q': {
802 V9fsQID *qidp = va_arg(ap, V9fsQID *);
803 offset += pdu_marshal(pdu, offset, "bdq",
804 qidp->type, qidp->version, qidp->path);
805 break;
807 case 'S': {
808 V9fsStat *statp = va_arg(ap, V9fsStat *);
809 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
810 statp->size, statp->type, statp->dev,
811 &statp->qid, statp->mode, statp->atime,
812 statp->mtime, statp->length, &statp->name,
813 &statp->uid, &statp->gid, &statp->muid,
814 &statp->extension, statp->n_uid,
815 statp->n_gid, statp->n_muid);
816 break;
818 case 'A': {
819 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
820 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
821 statp->st_result_mask,
822 &statp->qid, statp->st_mode,
823 statp->st_uid, statp->st_gid,
824 statp->st_nlink, statp->st_rdev,
825 statp->st_size, statp->st_blksize, statp->st_blocks,
826 statp->st_atime_sec, statp->st_atime_nsec,
827 statp->st_mtime_sec, statp->st_mtime_nsec,
828 statp->st_ctime_sec, statp->st_ctime_nsec,
829 statp->st_btime_sec, statp->st_btime_nsec,
830 statp->st_gen, statp->st_data_version);
831 break;
833 default:
834 break;
837 va_end(ap);
839 return offset - old_offset;
842 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
844 int8_t id = pdu->id + 1; /* Response */
846 if (len < 0) {
847 int err = -len;
848 len = 7;
850 if (s->proto_version != V9FS_PROTO_2000L) {
851 V9fsString str;
853 str.data = strerror(err);
854 str.size = strlen(str.data);
856 len += pdu_marshal(pdu, len, "s", &str);
857 id = P9_RERROR;
860 len += pdu_marshal(pdu, len, "d", err);
862 if (s->proto_version == V9FS_PROTO_2000L) {
863 id = P9_RLERROR;
867 /* fill out the header */
868 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
870 /* keep these in sync */
871 pdu->size = len;
872 pdu->id = id;
874 /* push onto queue and notify */
875 virtqueue_push(s->vq, &pdu->elem, len);
877 /* FIXME: we should batch these completions */
878 virtio_notify(&s->vdev, s->vq);
880 free_pdu(s, pdu);
883 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
885 mode_t ret;
887 ret = mode & 0777;
888 if (mode & P9_STAT_MODE_DIR) {
889 ret |= S_IFDIR;
892 if (mode & P9_STAT_MODE_SYMLINK) {
893 ret |= S_IFLNK;
895 if (mode & P9_STAT_MODE_SOCKET) {
896 ret |= S_IFSOCK;
898 if (mode & P9_STAT_MODE_NAMED_PIPE) {
899 ret |= S_IFIFO;
901 if (mode & P9_STAT_MODE_DEVICE) {
902 if (extension && extension->data[0] == 'c') {
903 ret |= S_IFCHR;
904 } else {
905 ret |= S_IFBLK;
909 if (!(ret&~0777)) {
910 ret |= S_IFREG;
913 if (mode & P9_STAT_MODE_SETUID) {
914 ret |= S_ISUID;
916 if (mode & P9_STAT_MODE_SETGID) {
917 ret |= S_ISGID;
919 if (mode & P9_STAT_MODE_SETVTX) {
920 ret |= S_ISVTX;
923 return ret;
926 static int donttouch_stat(V9fsStat *stat)
928 if (stat->type == -1 &&
929 stat->dev == -1 &&
930 stat->qid.type == -1 &&
931 stat->qid.version == -1 &&
932 stat->qid.path == -1 &&
933 stat->mode == -1 &&
934 stat->atime == -1 &&
935 stat->mtime == -1 &&
936 stat->length == -1 &&
937 !stat->name.size &&
938 !stat->uid.size &&
939 !stat->gid.size &&
940 !stat->muid.size &&
941 stat->n_uid == -1 &&
942 stat->n_gid == -1 &&
943 stat->n_muid == -1) {
944 return 1;
947 return 0;
950 static void v9fs_stat_free(V9fsStat *stat)
952 v9fs_string_free(&stat->name);
953 v9fs_string_free(&stat->uid);
954 v9fs_string_free(&stat->gid);
955 v9fs_string_free(&stat->muid);
956 v9fs_string_free(&stat->extension);
959 static uint32_t stat_to_v9mode(const struct stat *stbuf)
961 uint32_t mode;
963 mode = stbuf->st_mode & 0777;
964 if (S_ISDIR(stbuf->st_mode)) {
965 mode |= P9_STAT_MODE_DIR;
968 if (S_ISLNK(stbuf->st_mode)) {
969 mode |= P9_STAT_MODE_SYMLINK;
972 if (S_ISSOCK(stbuf->st_mode)) {
973 mode |= P9_STAT_MODE_SOCKET;
976 if (S_ISFIFO(stbuf->st_mode)) {
977 mode |= P9_STAT_MODE_NAMED_PIPE;
980 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
981 mode |= P9_STAT_MODE_DEVICE;
984 if (stbuf->st_mode & S_ISUID) {
985 mode |= P9_STAT_MODE_SETUID;
988 if (stbuf->st_mode & S_ISGID) {
989 mode |= P9_STAT_MODE_SETGID;
992 if (stbuf->st_mode & S_ISVTX) {
993 mode |= P9_STAT_MODE_SETVTX;
996 return mode;
999 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1000 const struct stat *stbuf,
1001 V9fsStat *v9stat)
1003 int err;
1004 const char *str;
1006 memset(v9stat, 0, sizeof(*v9stat));
1008 stat_to_qid(stbuf, &v9stat->qid);
1009 v9stat->mode = stat_to_v9mode(stbuf);
1010 v9stat->atime = stbuf->st_atime;
1011 v9stat->mtime = stbuf->st_mtime;
1012 v9stat->length = stbuf->st_size;
1014 v9fs_string_null(&v9stat->uid);
1015 v9fs_string_null(&v9stat->gid);
1016 v9fs_string_null(&v9stat->muid);
1018 v9stat->n_uid = stbuf->st_uid;
1019 v9stat->n_gid = stbuf->st_gid;
1020 v9stat->n_muid = 0;
1022 v9fs_string_null(&v9stat->extension);
1024 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1025 err = v9fs_do_readlink(s, name, &v9stat->extension);
1026 if (err == -1) {
1027 err = -errno;
1028 return err;
1030 v9stat->extension.data[err] = 0;
1031 v9stat->extension.size = err;
1032 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1033 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1034 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1035 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1036 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1037 v9fs_string_sprintf(&v9stat->extension, "%s %u",
1038 "HARDLINKCOUNT", stbuf->st_nlink);
1041 str = strrchr(name->data, '/');
1042 if (str) {
1043 str += 1;
1044 } else {
1045 str = name->data;
1048 v9fs_string_sprintf(&v9stat->name, "%s", str);
1050 v9stat->size = 61 +
1051 v9fs_string_size(&v9stat->name) +
1052 v9fs_string_size(&v9stat->uid) +
1053 v9fs_string_size(&v9stat->gid) +
1054 v9fs_string_size(&v9stat->muid) +
1055 v9fs_string_size(&v9stat->extension);
1056 return 0;
1059 #define P9_STATS_MODE 0x00000001ULL
1060 #define P9_STATS_NLINK 0x00000002ULL
1061 #define P9_STATS_UID 0x00000004ULL
1062 #define P9_STATS_GID 0x00000008ULL
1063 #define P9_STATS_RDEV 0x00000010ULL
1064 #define P9_STATS_ATIME 0x00000020ULL
1065 #define P9_STATS_MTIME 0x00000040ULL
1066 #define P9_STATS_CTIME 0x00000080ULL
1067 #define P9_STATS_INO 0x00000100ULL
1068 #define P9_STATS_SIZE 0x00000200ULL
1069 #define P9_STATS_BLOCKS 0x00000400ULL
1071 #define P9_STATS_BTIME 0x00000800ULL
1072 #define P9_STATS_GEN 0x00001000ULL
1073 #define P9_STATS_DATA_VERSION 0x00002000ULL
1075 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1076 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1079 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1080 V9fsStatDotl *v9lstat)
1082 memset(v9lstat, 0, sizeof(*v9lstat));
1084 v9lstat->st_mode = stbuf->st_mode;
1085 v9lstat->st_nlink = stbuf->st_nlink;
1086 v9lstat->st_uid = stbuf->st_uid;
1087 v9lstat->st_gid = stbuf->st_gid;
1088 v9lstat->st_rdev = stbuf->st_rdev;
1089 v9lstat->st_size = stbuf->st_size;
1090 v9lstat->st_blksize = stbuf->st_blksize;
1091 v9lstat->st_blocks = stbuf->st_blocks;
1092 v9lstat->st_atime_sec = stbuf->st_atime;
1093 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1094 v9lstat->st_mtime_sec = stbuf->st_mtime;
1095 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1096 v9lstat->st_ctime_sec = stbuf->st_ctime;
1097 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1098 /* Currently we only support BASIC fields in stat */
1099 v9lstat->st_result_mask = P9_STATS_BASIC;
1101 stat_to_qid(stbuf, &v9lstat->qid);
1104 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1106 while (len && *iovcnt) {
1107 if (len < sg->iov_len) {
1108 sg->iov_len -= len;
1109 sg->iov_base += len;
1110 len = 0;
1111 } else {
1112 len -= sg->iov_len;
1113 sg++;
1114 *iovcnt -= 1;
1118 return sg;
1121 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1123 int i;
1124 int total = 0;
1126 for (i = 0; i < *cnt; i++) {
1127 if ((total + sg[i].iov_len) > cap) {
1128 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1129 i++;
1130 break;
1132 total += sg[i].iov_len;
1135 *cnt = i;
1137 return sg;
1140 static void print_sg(struct iovec *sg, int cnt)
1142 int i;
1144 printf("sg[%d]: {", cnt);
1145 for (i = 0; i < cnt; i++) {
1146 if (i) {
1147 printf(", ");
1149 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1151 printf("}\n");
1154 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1156 V9fsString str;
1157 v9fs_string_init(&str);
1158 v9fs_string_copy(&str, dst);
1159 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1160 v9fs_string_free(&str);
1163 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1165 V9fsString version;
1166 size_t offset = 7;
1168 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1170 if (!strcmp(version.data, "9P2000.u")) {
1171 s->proto_version = V9FS_PROTO_2000U;
1172 } else if (!strcmp(version.data, "9P2000.L")) {
1173 s->proto_version = V9FS_PROTO_2000L;
1174 } else {
1175 v9fs_string_sprintf(&version, "unknown");
1178 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1179 complete_pdu(s, pdu, offset);
1181 v9fs_string_free(&version);
1184 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1186 int32_t fid, afid, n_uname;
1187 V9fsString uname, aname;
1188 V9fsFidState *fidp;
1189 V9fsQID qid;
1190 size_t offset = 7;
1191 ssize_t err;
1193 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1195 fidp = alloc_fid(s, fid);
1196 if (fidp == NULL) {
1197 err = -EINVAL;
1198 goto out;
1201 fidp->uid = n_uname;
1203 v9fs_string_sprintf(&fidp->path, "%s", "/");
1204 err = fid_to_qid(s, fidp, &qid);
1205 if (err) {
1206 err = -EINVAL;
1207 free_fid(s, fid);
1208 goto out;
1211 offset += pdu_marshal(pdu, offset, "Q", &qid);
1213 err = offset;
1214 out:
1215 complete_pdu(s, pdu, err);
1216 v9fs_string_free(&uname);
1217 v9fs_string_free(&aname);
1220 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1222 if (err == -1) {
1223 err = -errno;
1224 goto out;
1227 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1228 if (err) {
1229 goto out;
1231 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1232 err = vs->offset;
1234 out:
1235 complete_pdu(s, vs->pdu, err);
1236 v9fs_stat_free(&vs->v9stat);
1237 qemu_free(vs);
1240 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1242 int32_t fid;
1243 V9fsStatState *vs;
1244 ssize_t err = 0;
1246 vs = qemu_malloc(sizeof(*vs));
1247 vs->pdu = pdu;
1248 vs->offset = 7;
1250 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1252 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1254 vs->fidp = lookup_fid(s, fid);
1255 if (vs->fidp == NULL) {
1256 err = -ENOENT;
1257 goto out;
1260 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1261 v9fs_stat_post_lstat(s, vs, err);
1262 return;
1264 out:
1265 complete_pdu(s, vs->pdu, err);
1266 v9fs_stat_free(&vs->v9stat);
1267 qemu_free(vs);
1270 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1271 int err)
1273 if (err == -1) {
1274 err = -errno;
1275 goto out;
1278 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1279 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1280 err = vs->offset;
1282 out:
1283 complete_pdu(s, vs->pdu, err);
1284 qemu_free(vs);
1287 static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1289 int32_t fid;
1290 V9fsStatStateDotl *vs;
1291 ssize_t err = 0;
1292 V9fsFidState *fidp;
1293 uint64_t request_mask;
1295 vs = qemu_malloc(sizeof(*vs));
1296 vs->pdu = pdu;
1297 vs->offset = 7;
1299 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1301 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1303 fidp = lookup_fid(s, fid);
1304 if (fidp == NULL) {
1305 err = -ENOENT;
1306 goto out;
1309 /* Currently we only support BASIC fields in stat, so there is no
1310 * need to look at request_mask.
1312 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1313 v9fs_getattr_post_lstat(s, vs, err);
1314 return;
1316 out:
1317 complete_pdu(s, vs->pdu, err);
1318 qemu_free(vs);
1321 /* From Linux kernel code */
1322 #define ATTR_MODE (1 << 0)
1323 #define ATTR_UID (1 << 1)
1324 #define ATTR_GID (1 << 2)
1325 #define ATTR_SIZE (1 << 3)
1326 #define ATTR_ATIME (1 << 4)
1327 #define ATTR_MTIME (1 << 5)
1328 #define ATTR_CTIME (1 << 6)
1329 #define ATTR_MASK 127
1330 #define ATTR_ATIME_SET (1 << 7)
1331 #define ATTR_MTIME_SET (1 << 8)
1333 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1334 int err)
1336 if (err == -1) {
1337 err = -errno;
1338 goto out;
1340 err = vs->offset;
1342 out:
1343 complete_pdu(s, vs->pdu, err);
1344 qemu_free(vs);
1347 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1349 if (err == -1) {
1350 err = -errno;
1351 goto out;
1354 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1355 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1357 v9fs_setattr_post_truncate(s, vs, err);
1358 return;
1360 out:
1361 complete_pdu(s, vs->pdu, err);
1362 qemu_free(vs);
1365 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1366 int err)
1368 if (err == -1) {
1369 err = -errno;
1370 goto out;
1373 /* If the only valid entry in iattr is ctime we can call
1374 * chown(-1,-1) to update the ctime of the file
1376 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1377 ((vs->v9iattr.valid & ATTR_CTIME)
1378 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1379 if (!(vs->v9iattr.valid & ATTR_UID)) {
1380 vs->v9iattr.uid = -1;
1382 if (!(vs->v9iattr.valid & ATTR_GID)) {
1383 vs->v9iattr.gid = -1;
1385 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1386 vs->v9iattr.gid);
1388 v9fs_setattr_post_chown(s, vs, err);
1389 return;
1391 out:
1392 complete_pdu(s, vs->pdu, err);
1393 qemu_free(vs);
1396 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1398 if (err == -1) {
1399 err = -errno;
1400 goto out;
1403 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1404 struct timespec times[2];
1405 if (vs->v9iattr.valid & ATTR_ATIME) {
1406 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1407 times[0].tv_sec = vs->v9iattr.atime_sec;
1408 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1409 } else {
1410 times[0].tv_nsec = UTIME_NOW;
1412 } else {
1413 times[0].tv_nsec = UTIME_OMIT;
1416 if (vs->v9iattr.valid & ATTR_MTIME) {
1417 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1418 times[1].tv_sec = vs->v9iattr.mtime_sec;
1419 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1420 } else {
1421 times[1].tv_nsec = UTIME_NOW;
1423 } else {
1424 times[1].tv_nsec = UTIME_OMIT;
1426 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1428 v9fs_setattr_post_utimensat(s, vs, err);
1429 return;
1431 out:
1432 complete_pdu(s, vs->pdu, err);
1433 qemu_free(vs);
1436 static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1438 int32_t fid;
1439 V9fsSetattrState *vs;
1440 int err = 0;
1442 vs = qemu_malloc(sizeof(*vs));
1443 vs->pdu = pdu;
1444 vs->offset = 7;
1446 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1448 vs->fidp = lookup_fid(s, fid);
1449 if (vs->fidp == NULL) {
1450 err = -EINVAL;
1451 goto out;
1454 if (vs->v9iattr.valid & ATTR_MODE) {
1455 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1458 v9fs_setattr_post_chmod(s, vs, err);
1459 return;
1461 out:
1462 complete_pdu(s, vs->pdu, err);
1463 qemu_free(vs);
1466 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1468 complete_pdu(s, vs->pdu, err);
1470 if (vs->nwnames) {
1471 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1472 v9fs_string_free(&vs->wnames[vs->name_idx]);
1475 qemu_free(vs->wnames);
1476 qemu_free(vs->qids);
1480 static void v9fs_walk_marshal(V9fsWalkState *vs)
1482 int i;
1483 vs->offset = 7;
1484 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1486 for (i = 0; i < vs->nwnames; i++) {
1487 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1491 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1492 int err)
1494 if (err == -1) {
1495 free_fid(s, vs->newfidp->fid);
1496 v9fs_string_free(&vs->path);
1497 err = -ENOENT;
1498 goto out;
1501 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1503 vs->name_idx++;
1504 if (vs->name_idx < vs->nwnames) {
1505 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1506 vs->wnames[vs->name_idx].data);
1507 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1509 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1510 v9fs_walk_post_newfid_lstat(s, vs, err);
1511 return;
1514 v9fs_string_free(&vs->path);
1515 v9fs_walk_marshal(vs);
1516 err = vs->offset;
1517 out:
1518 v9fs_walk_complete(s, vs, err);
1521 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1522 int err)
1524 if (err == -1) {
1525 v9fs_string_free(&vs->path);
1526 err = -ENOENT;
1527 goto out;
1530 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1531 vs->name_idx++;
1532 if (vs->name_idx < vs->nwnames) {
1534 v9fs_string_sprintf(&vs->path, "%s/%s",
1535 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1536 v9fs_string_copy(&vs->fidp->path, &vs->path);
1538 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1539 v9fs_walk_post_oldfid_lstat(s, vs, err);
1540 return;
1543 v9fs_string_free(&vs->path);
1544 v9fs_walk_marshal(vs);
1545 err = vs->offset;
1546 out:
1547 v9fs_walk_complete(s, vs, err);
1550 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1552 int32_t fid, newfid;
1553 V9fsWalkState *vs;
1554 int err = 0;
1555 int i;
1557 vs = qemu_malloc(sizeof(*vs));
1558 vs->pdu = pdu;
1559 vs->wnames = NULL;
1560 vs->qids = NULL;
1561 vs->offset = 7;
1563 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1564 &newfid, &vs->nwnames);
1566 if (vs->nwnames) {
1567 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1569 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1571 for (i = 0; i < vs->nwnames; i++) {
1572 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1573 &vs->wnames[i]);
1577 vs->fidp = lookup_fid(s, fid);
1578 if (vs->fidp == NULL) {
1579 err = -ENOENT;
1580 goto out;
1583 /* FIXME: is this really valid? */
1584 if (fid == newfid) {
1586 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1587 v9fs_string_init(&vs->path);
1588 vs->name_idx = 0;
1590 if (vs->name_idx < vs->nwnames) {
1591 v9fs_string_sprintf(&vs->path, "%s/%s",
1592 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1593 v9fs_string_copy(&vs->fidp->path, &vs->path);
1595 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1596 v9fs_walk_post_oldfid_lstat(s, vs, err);
1597 return;
1599 } else {
1600 vs->newfidp = alloc_fid(s, newfid);
1601 if (vs->newfidp == NULL) {
1602 err = -EINVAL;
1603 goto out;
1606 vs->newfidp->uid = vs->fidp->uid;
1607 v9fs_string_init(&vs->path);
1608 vs->name_idx = 0;
1609 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1611 if (vs->name_idx < vs->nwnames) {
1612 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1613 vs->wnames[vs->name_idx].data);
1614 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1616 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1617 v9fs_walk_post_newfid_lstat(s, vs, err);
1618 return;
1622 v9fs_walk_marshal(vs);
1623 err = vs->offset;
1624 out:
1625 v9fs_walk_complete(s, vs, err);
1628 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1630 struct statfs stbuf;
1631 int32_t iounit = 0;
1634 * iounit should be multiples of f_bsize (host filesystem block size
1635 * and as well as less than (client msize - P9_IOHDRSZ))
1637 if (!v9fs_do_statfs(s, name, &stbuf)) {
1638 iounit = stbuf.f_bsize;
1639 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1642 if (!iounit) {
1643 iounit = s->msize - P9_IOHDRSZ;
1645 return iounit;
1648 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1650 if (vs->fidp->fs.dir == NULL) {
1651 err = -errno;
1652 goto out;
1654 vs->fidp->fid_type = P9_FID_DIR;
1655 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1656 err = vs->offset;
1657 out:
1658 complete_pdu(s, vs->pdu, err);
1659 qemu_free(vs);
1663 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1665 int err;
1666 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1667 err = vs->offset;
1668 complete_pdu(s, vs->pdu, err);
1669 qemu_free(vs);
1672 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1674 if (vs->fidp->fs.fd == -1) {
1675 err = -errno;
1676 goto out;
1678 vs->fidp->fid_type = P9_FID_FILE;
1679 vs->iounit = get_iounit(s, &vs->fidp->path);
1680 v9fs_open_post_getiounit(s, vs);
1681 return;
1682 out:
1683 complete_pdu(s, vs->pdu, err);
1684 qemu_free(vs);
1687 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1689 int flags;
1691 if (err) {
1692 err = -errno;
1693 goto out;
1696 stat_to_qid(&vs->stbuf, &vs->qid);
1698 if (S_ISDIR(vs->stbuf.st_mode)) {
1699 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1700 v9fs_open_post_opendir(s, vs, err);
1701 } else {
1702 if (s->proto_version == V9FS_PROTO_2000L) {
1703 flags = vs->mode;
1704 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1705 } else {
1706 flags = omode_to_uflags(vs->mode);
1708 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1709 v9fs_open_post_open(s, vs, err);
1711 return;
1712 out:
1713 complete_pdu(s, vs->pdu, err);
1714 qemu_free(vs);
1717 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1719 int32_t fid;
1720 V9fsOpenState *vs;
1721 ssize_t err = 0;
1723 vs = qemu_malloc(sizeof(*vs));
1724 vs->pdu = pdu;
1725 vs->offset = 7;
1726 vs->mode = 0;
1728 if (s->proto_version == V9FS_PROTO_2000L) {
1729 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1730 } else {
1731 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1734 vs->fidp = lookup_fid(s, fid);
1735 if (vs->fidp == NULL) {
1736 err = -ENOENT;
1737 goto out;
1740 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1742 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1744 v9fs_open_post_lstat(s, vs, err);
1745 return;
1746 out:
1747 complete_pdu(s, pdu, err);
1748 qemu_free(vs);
1751 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1753 if (err == 0) {
1754 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1755 stat_to_qid(&vs->stbuf, &vs->qid);
1756 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1757 &vs->iounit);
1758 err = vs->offset;
1759 } else {
1760 vs->fidp->fid_type = P9_FID_NONE;
1761 close(vs->fidp->fs.fd);
1762 err = -errno;
1765 complete_pdu(s, vs->pdu, err);
1766 v9fs_string_free(&vs->name);
1767 v9fs_string_free(&vs->fullname);
1768 qemu_free(vs);
1771 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1772 int err)
1774 if (err) {
1775 err = -errno;
1776 goto out;
1778 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1780 out:
1781 v9fs_post_lcreate(s, vs, err);
1784 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1785 int err)
1787 if (vs->fidp->fs.fd == -1) {
1788 err = -errno;
1789 goto out;
1791 vs->fidp->fid_type = P9_FID_FILE;
1792 vs->iounit = get_iounit(s, &vs->fullname);
1793 v9fs_lcreate_post_get_iounit(s, vs, err);
1794 return;
1796 out:
1797 v9fs_post_lcreate(s, vs, err);
1800 static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1802 int32_t dfid, flags, mode;
1803 gid_t gid;
1804 V9fsLcreateState *vs;
1805 ssize_t err = 0;
1807 vs = qemu_malloc(sizeof(*vs));
1808 vs->pdu = pdu;
1809 vs->offset = 7;
1811 v9fs_string_init(&vs->fullname);
1813 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1814 &mode, &gid);
1816 vs->fidp = lookup_fid(s, dfid);
1817 if (vs->fidp == NULL) {
1818 err = -ENOENT;
1819 goto out;
1822 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1823 vs->name.data);
1825 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1826 gid, flags, mode);
1827 v9fs_lcreate_post_do_open2(s, vs, err);
1828 return;
1830 out:
1831 complete_pdu(s, vs->pdu, err);
1832 v9fs_string_free(&vs->name);
1833 qemu_free(vs);
1836 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1838 int32_t fid;
1839 size_t offset = 7;
1840 int err;
1842 pdu_unmarshal(pdu, offset, "d", &fid);
1844 err = free_fid(s, fid);
1845 if (err < 0) {
1846 goto out;
1849 offset = 7;
1850 err = offset;
1851 out:
1852 complete_pdu(s, pdu, err);
1855 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1857 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1859 if (err) {
1860 goto out;
1862 v9fs_stat_free(&vs->v9stat);
1863 v9fs_string_free(&vs->name);
1864 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1865 vs->offset += vs->count;
1866 err = vs->offset;
1867 out:
1868 complete_pdu(s, vs->pdu, err);
1869 qemu_free(vs);
1870 return;
1873 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1874 ssize_t err)
1876 if (err) {
1877 err = -errno;
1878 goto out;
1880 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1881 if (err) {
1882 goto out;
1885 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1886 &vs->v9stat);
1887 if ((vs->len != (vs->v9stat.size + 2)) ||
1888 ((vs->count + vs->len) > vs->max_count)) {
1889 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1890 v9fs_read_post_seekdir(s, vs, err);
1891 return;
1893 vs->count += vs->len;
1894 v9fs_stat_free(&vs->v9stat);
1895 v9fs_string_free(&vs->name);
1896 vs->dir_pos = vs->dent->d_off;
1897 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1898 v9fs_read_post_readdir(s, vs, err);
1899 return;
1900 out:
1901 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1902 v9fs_read_post_seekdir(s, vs, err);
1903 return;
1907 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1909 if (vs->dent) {
1910 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1911 v9fs_string_init(&vs->name);
1912 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1913 vs->dent->d_name);
1914 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1915 v9fs_read_post_dir_lstat(s, vs, err);
1916 return;
1919 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1920 vs->offset += vs->count;
1921 err = vs->offset;
1922 complete_pdu(s, vs->pdu, err);
1923 qemu_free(vs);
1924 return;
1927 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1929 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1930 v9fs_read_post_readdir(s, vs, err);
1931 return;
1934 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1935 ssize_t err)
1937 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1938 v9fs_read_post_telldir(s, vs, err);
1939 return;
1942 static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1944 if (err < 0) {
1945 /* IO error return the error */
1946 err = -errno;
1947 goto out;
1949 vs->total += vs->len;
1950 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1951 if (vs->total < vs->count && vs->len > 0) {
1952 do {
1953 if (0) {
1954 print_sg(vs->sg, vs->cnt);
1956 vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1957 } while (vs->len == -1 && errno == EINTR);
1958 if (vs->len == -1) {
1959 err = -errno;
1961 v9fs_read_post_readv(s, vs, err);
1962 return;
1964 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1965 vs->offset += vs->count;
1966 err = vs->offset;
1968 out:
1969 complete_pdu(s, vs->pdu, err);
1970 qemu_free(vs);
1973 static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1975 if (err == -1) {
1976 err = -errno;
1977 goto out;
1979 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1981 if (vs->total < vs->count) {
1982 do {
1983 if (0) {
1984 print_sg(vs->sg, vs->cnt);
1986 vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1987 } while (vs->len == -1 && errno == EINTR);
1988 if (vs->len == -1) {
1989 err = -errno;
1991 v9fs_read_post_readv(s, vs, err);
1992 return;
1994 out:
1995 complete_pdu(s, vs->pdu, err);
1996 qemu_free(vs);
1999 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2001 ssize_t err = 0;
2002 int read_count;
2003 int64_t xattr_len;
2005 xattr_len = vs->fidp->fs.xattr.len;
2006 read_count = xattr_len - vs->off;
2007 if (read_count > vs->count) {
2008 read_count = vs->count;
2009 } else if (read_count < 0) {
2011 * read beyond XATTR value
2013 read_count = 0;
2015 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2016 vs->offset += pdu_pack(vs->pdu, vs->offset,
2017 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2018 read_count);
2019 err = vs->offset;
2020 complete_pdu(s, vs->pdu, err);
2021 qemu_free(vs);
2024 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2026 int32_t fid;
2027 V9fsReadState *vs;
2028 ssize_t err = 0;
2030 vs = qemu_malloc(sizeof(*vs));
2031 vs->pdu = pdu;
2032 vs->offset = 7;
2033 vs->total = 0;
2034 vs->len = 0;
2035 vs->count = 0;
2037 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2039 vs->fidp = lookup_fid(s, fid);
2040 if (vs->fidp == NULL) {
2041 err = -EINVAL;
2042 goto out;
2045 if (vs->fidp->fid_type == P9_FID_DIR) {
2046 vs->max_count = vs->count;
2047 vs->count = 0;
2048 if (vs->off == 0) {
2049 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2051 v9fs_read_post_rewinddir(s, vs, err);
2052 return;
2053 } else if (vs->fidp->fid_type == P9_FID_FILE) {
2054 vs->sg = vs->iov;
2055 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2056 err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2057 v9fs_read_post_lseek(s, vs, err);
2058 return;
2059 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2060 v9fs_xattr_read(s, vs);
2061 return;
2062 } else {
2063 err = -EINVAL;
2065 out:
2066 complete_pdu(s, pdu, err);
2067 qemu_free(vs);
2070 typedef struct V9fsReadDirState {
2071 V9fsPDU *pdu;
2072 V9fsFidState *fidp;
2073 V9fsQID qid;
2074 off_t saved_dir_pos;
2075 struct dirent *dent;
2076 int32_t count;
2077 int32_t max_count;
2078 size_t offset;
2079 int64_t initial_offset;
2080 V9fsString name;
2081 } V9fsReadDirState;
2083 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2085 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2086 vs->offset += vs->count;
2087 complete_pdu(s, vs->pdu, vs->offset);
2088 qemu_free(vs);
2089 return;
2092 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2093 * size of type (1) + size of name.size (2) + strlen(name.data)
2095 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2097 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2099 int len;
2100 size_t size;
2102 if (vs->dent) {
2103 v9fs_string_init(&vs->name);
2104 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2106 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2107 /* Ran out of buffer. Set dir back to old position and return */
2108 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2109 v9fs_readdir_post_seekdir(s, vs);
2110 return;
2113 /* Fill up just the path field of qid because the client uses
2114 * only that. To fill the entire qid structure we will have
2115 * to stat each dirent found, which is expensive
2117 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2118 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2119 /* Fill the other fields with dummy values */
2120 vs->qid.type = 0;
2121 vs->qid.version = 0;
2123 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2124 &vs->qid, vs->dent->d_off,
2125 vs->dent->d_type, &vs->name);
2126 vs->count += len;
2127 v9fs_string_free(&vs->name);
2128 vs->saved_dir_pos = vs->dent->d_off;
2129 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2130 v9fs_readdir_post_readdir(s, vs);
2131 return;
2134 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2135 vs->offset += vs->count;
2136 complete_pdu(s, vs->pdu, vs->offset);
2137 qemu_free(vs);
2138 return;
2141 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2143 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2144 v9fs_readdir_post_readdir(s, vs);
2145 return;
2148 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2150 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2151 v9fs_readdir_post_telldir(s, vs);
2152 return;
2155 static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2157 int32_t fid;
2158 V9fsReadDirState *vs;
2159 ssize_t err = 0;
2160 size_t offset = 7;
2162 vs = qemu_malloc(sizeof(*vs));
2163 vs->pdu = pdu;
2164 vs->offset = 7;
2165 vs->count = 0;
2167 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2168 &vs->max_count);
2170 vs->fidp = lookup_fid(s, fid);
2171 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2172 err = -EINVAL;
2173 goto out;
2176 if (vs->initial_offset == 0) {
2177 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2178 } else {
2179 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2182 v9fs_readdir_post_setdir(s, vs);
2183 return;
2185 out:
2186 complete_pdu(s, pdu, err);
2187 qemu_free(vs);
2188 return;
2191 static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
2192 ssize_t err)
2194 if (err < 0) {
2195 /* IO error return the error */
2196 err = -errno;
2197 goto out;
2199 vs->total += vs->len;
2200 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2201 if (vs->total < vs->count && vs->len > 0) {
2202 do {
2203 if (0) {
2204 print_sg(vs->sg, vs->cnt);
2206 vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2207 } while (vs->len == -1 && errno == EINTR);
2208 if (vs->len == -1) {
2209 err = -errno;
2211 v9fs_write_post_writev(s, vs, err);
2212 return;
2214 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2216 err = vs->offset;
2217 out:
2218 complete_pdu(s, vs->pdu, err);
2219 qemu_free(vs);
2222 static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
2224 if (err == -1) {
2225 err = -errno;
2226 goto out;
2228 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2230 if (vs->total < vs->count) {
2231 do {
2232 if (0) {
2233 print_sg(vs->sg, vs->cnt);
2235 vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2236 } while (vs->len == -1 && errno == EINTR);
2237 if (vs->len == -1) {
2238 err = -errno;
2240 v9fs_write_post_writev(s, vs, err);
2241 return;
2244 out:
2245 complete_pdu(s, vs->pdu, err);
2246 qemu_free(vs);
2249 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2251 int i, to_copy;
2252 ssize_t err = 0;
2253 int write_count;
2254 int64_t xattr_len;
2256 xattr_len = vs->fidp->fs.xattr.len;
2257 write_count = xattr_len - vs->off;
2258 if (write_count > vs->count) {
2259 write_count = vs->count;
2260 } else if (write_count < 0) {
2262 * write beyond XATTR value len specified in
2263 * xattrcreate
2265 err = -ENOSPC;
2266 goto out;
2268 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2269 err = vs->offset;
2270 vs->fidp->fs.xattr.copied_len += write_count;
2272 * Now copy the content from sg list
2274 for (i = 0; i < vs->cnt; i++) {
2275 if (write_count > vs->sg[i].iov_len) {
2276 to_copy = vs->sg[i].iov_len;
2277 } else {
2278 to_copy = write_count;
2280 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2281 vs->sg[i].iov_base, to_copy);
2282 /* updating vs->off since we are not using below */
2283 vs->off += to_copy;
2284 write_count -= to_copy;
2286 out:
2287 complete_pdu(s, vs->pdu, err);
2288 qemu_free(vs);
2291 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2293 int32_t fid;
2294 V9fsWriteState *vs;
2295 ssize_t err;
2297 vs = qemu_malloc(sizeof(*vs));
2299 vs->pdu = pdu;
2300 vs->offset = 7;
2301 vs->sg = vs->iov;
2302 vs->total = 0;
2303 vs->len = 0;
2305 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2306 vs->sg, &vs->cnt);
2308 vs->fidp = lookup_fid(s, fid);
2309 if (vs->fidp == NULL) {
2310 err = -EINVAL;
2311 goto out;
2314 if (vs->fidp->fid_type == P9_FID_FILE) {
2315 if (vs->fidp->fs.fd == -1) {
2316 err = -EINVAL;
2317 goto out;
2319 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2321 * setxattr operation
2323 v9fs_xattr_write(s, vs);
2324 return;
2325 } else {
2326 err = -EINVAL;
2327 goto out;
2329 err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2331 v9fs_write_post_lseek(s, vs, err);
2332 return;
2334 out:
2335 complete_pdu(s, vs->pdu, err);
2336 qemu_free(vs);
2339 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2341 int err;
2342 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2343 stat_to_qid(&vs->stbuf, &vs->qid);
2345 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2346 err = vs->offset;
2348 complete_pdu(s, vs->pdu, err);
2349 v9fs_string_free(&vs->name);
2350 v9fs_string_free(&vs->extension);
2351 v9fs_string_free(&vs->fullname);
2352 qemu_free(vs);
2355 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2357 if (err == 0) {
2358 vs->iounit = get_iounit(s, &vs->fidp->path);
2359 v9fs_create_post_getiounit(s, vs);
2360 return;
2363 complete_pdu(s, vs->pdu, err);
2364 v9fs_string_free(&vs->name);
2365 v9fs_string_free(&vs->extension);
2366 v9fs_string_free(&vs->fullname);
2367 qemu_free(vs);
2370 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2372 if (err) {
2373 err = -errno;
2375 v9fs_post_create(s, vs, err);
2378 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2379 int err)
2381 if (!vs->fidp->fs.dir) {
2382 err = -errno;
2384 vs->fidp->fid_type = P9_FID_DIR;
2385 v9fs_post_create(s, vs, err);
2388 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2389 int err)
2391 if (err) {
2392 err = -errno;
2393 goto out;
2396 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2397 v9fs_create_post_opendir(s, vs, err);
2398 return;
2400 out:
2401 v9fs_post_create(s, vs, err);
2404 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2406 if (err) {
2407 err = -errno;
2408 goto out;
2411 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2412 v9fs_create_post_dir_lstat(s, vs, err);
2413 return;
2415 out:
2416 v9fs_post_create(s, vs, err);
2419 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2421 if (err) {
2422 vs->fidp->fid_type = P9_FID_NONE;
2423 close(vs->fidp->fs.fd);
2424 err = -errno;
2426 v9fs_post_create(s, vs, err);
2427 return;
2430 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2432 if (vs->fidp->fs.fd == -1) {
2433 err = -errno;
2434 goto out;
2436 vs->fidp->fid_type = P9_FID_FILE;
2437 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2438 v9fs_create_post_fstat(s, vs, err);
2440 return;
2442 out:
2443 v9fs_post_create(s, vs, err);
2447 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2450 if (err == 0 || errno != ENOENT) {
2451 err = -errno;
2452 goto out;
2455 if (vs->perm & P9_STAT_MODE_DIR) {
2456 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2457 vs->fidp->uid, -1);
2458 v9fs_create_post_mkdir(s, vs, err);
2459 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2460 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2461 vs->fullname.data, -1);
2462 v9fs_create_post_perms(s, vs, err);
2463 } else if (vs->perm & P9_STAT_MODE_LINK) {
2464 int32_t nfid = atoi(vs->extension.data);
2465 V9fsFidState *nfidp = lookup_fid(s, nfid);
2466 if (nfidp == NULL) {
2467 err = -errno;
2468 v9fs_post_create(s, vs, err);
2470 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2471 v9fs_create_post_perms(s, vs, err);
2472 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2473 char ctype;
2474 uint32_t major, minor;
2475 mode_t nmode = 0;
2477 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2478 &minor) != 3) {
2479 err = -errno;
2480 v9fs_post_create(s, vs, err);
2483 switch (ctype) {
2484 case 'c':
2485 nmode = S_IFCHR;
2486 break;
2487 case 'b':
2488 nmode = S_IFBLK;
2489 break;
2490 default:
2491 err = -EIO;
2492 v9fs_post_create(s, vs, err);
2495 nmode |= vs->perm & 0777;
2496 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2497 makedev(major, minor), vs->fidp->uid, -1);
2498 v9fs_create_post_perms(s, vs, err);
2499 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2500 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2501 0, vs->fidp->uid, -1);
2502 v9fs_post_create(s, vs, err);
2503 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2504 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2505 0, vs->fidp->uid, -1);
2506 v9fs_post_create(s, vs, err);
2507 } else {
2508 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2509 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2511 v9fs_create_post_open2(s, vs, err);
2514 return;
2516 out:
2517 v9fs_post_create(s, vs, err);
2520 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2522 int32_t fid;
2523 V9fsCreateState *vs;
2524 int err = 0;
2526 vs = qemu_malloc(sizeof(*vs));
2527 vs->pdu = pdu;
2528 vs->offset = 7;
2530 v9fs_string_init(&vs->fullname);
2532 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2533 &vs->perm, &vs->mode, &vs->extension);
2535 vs->fidp = lookup_fid(s, fid);
2536 if (vs->fidp == NULL) {
2537 err = -EINVAL;
2538 goto out;
2541 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2542 vs->name.data);
2544 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2545 v9fs_create_post_lstat(s, vs, err);
2546 return;
2548 out:
2549 complete_pdu(s, vs->pdu, err);
2550 v9fs_string_free(&vs->name);
2551 v9fs_string_free(&vs->extension);
2552 qemu_free(vs);
2555 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2557 if (err == 0) {
2558 stat_to_qid(&vs->stbuf, &vs->qid);
2559 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2560 err = vs->offset;
2561 } else {
2562 err = -errno;
2564 complete_pdu(s, vs->pdu, err);
2565 v9fs_string_free(&vs->name);
2566 v9fs_string_free(&vs->symname);
2567 v9fs_string_free(&vs->fullname);
2568 qemu_free(vs);
2571 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2572 int err)
2574 if (err) {
2575 goto out;
2577 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2578 out:
2579 v9fs_post_symlink(s, vs, err);
2582 static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2584 int32_t dfid;
2585 V9fsSymlinkState *vs;
2586 int err = 0;
2587 gid_t gid;
2589 vs = qemu_malloc(sizeof(*vs));
2590 vs->pdu = pdu;
2591 vs->offset = 7;
2593 v9fs_string_init(&vs->fullname);
2595 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2596 &vs->symname, &gid);
2598 vs->dfidp = lookup_fid(s, dfid);
2599 if (vs->dfidp == NULL) {
2600 err = -EINVAL;
2601 goto out;
2604 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2605 vs->name.data);
2606 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2607 vs->fullname.data, gid);
2608 v9fs_symlink_post_do_symlink(s, vs, err);
2609 return;
2611 out:
2612 complete_pdu(s, vs->pdu, err);
2613 v9fs_string_free(&vs->name);
2614 v9fs_string_free(&vs->symname);
2615 qemu_free(vs);
2618 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2620 /* A nop call with no return */
2621 complete_pdu(s, pdu, 7);
2624 static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2626 int32_t dfid, oldfid;
2627 V9fsFidState *dfidp, *oldfidp;
2628 V9fsString name, fullname;
2629 size_t offset = 7;
2630 int err = 0;
2632 v9fs_string_init(&fullname);
2634 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2636 dfidp = lookup_fid(s, dfid);
2637 if (dfidp == NULL) {
2638 err = -errno;
2639 goto out;
2642 oldfidp = lookup_fid(s, oldfid);
2643 if (oldfidp == NULL) {
2644 err = -errno;
2645 goto out;
2648 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2649 err = offset;
2650 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2651 if (err) {
2652 err = -errno;
2654 v9fs_string_free(&fullname);
2656 out:
2657 v9fs_string_free(&name);
2658 complete_pdu(s, pdu, err);
2661 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2662 int err)
2664 if (err < 0) {
2665 err = -errno;
2666 } else {
2667 err = vs->offset;
2670 /* For TREMOVE we need to clunk the fid even on failed remove */
2671 free_fid(s, vs->fidp->fid);
2673 complete_pdu(s, vs->pdu, err);
2674 qemu_free(vs);
2677 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2679 int32_t fid;
2680 V9fsRemoveState *vs;
2681 int err = 0;
2683 vs = qemu_malloc(sizeof(*vs));
2684 vs->pdu = pdu;
2685 vs->offset = 7;
2687 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2689 vs->fidp = lookup_fid(s, fid);
2690 if (vs->fidp == NULL) {
2691 err = -EINVAL;
2692 goto out;
2695 err = v9fs_do_remove(s, &vs->fidp->path);
2696 v9fs_remove_post_remove(s, vs, err);
2697 return;
2699 out:
2700 complete_pdu(s, pdu, err);
2701 qemu_free(vs);
2704 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2706 if (err < 0) {
2707 goto out;
2710 err = vs->offset;
2712 out:
2713 v9fs_stat_free(&vs->v9stat);
2714 complete_pdu(s, vs->pdu, err);
2715 qemu_free(vs);
2718 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2720 if (err < 0) {
2721 goto out;
2723 if (vs->v9stat.length != -1) {
2724 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2725 err = -errno;
2728 v9fs_wstat_post_truncate(s, vs, err);
2729 return;
2731 out:
2732 v9fs_stat_free(&vs->v9stat);
2733 complete_pdu(s, vs->pdu, err);
2734 qemu_free(vs);
2737 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2739 int err = 0;
2740 char *old_name, *new_name;
2741 char *end;
2743 if (vs->newdirfid != -1) {
2744 V9fsFidState *dirfidp;
2745 dirfidp = lookup_fid(s, vs->newdirfid);
2747 if (dirfidp == NULL) {
2748 err = -ENOENT;
2749 goto out;
2752 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2754 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2756 strcpy(new_name, dirfidp->path.data);
2757 strcat(new_name, "/");
2758 strcat(new_name + dirfidp->path.size, vs->name.data);
2759 } else {
2760 old_name = vs->fidp->path.data;
2761 end = strrchr(old_name, '/');
2762 if (end) {
2763 end++;
2764 } else {
2765 end = old_name;
2767 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2769 strncat(new_name, old_name, end - old_name);
2770 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2773 v9fs_string_free(&vs->name);
2774 vs->name.data = qemu_strdup(new_name);
2775 vs->name.size = strlen(new_name);
2777 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2778 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2779 err = -errno;
2780 } else {
2781 V9fsFidState *fidp;
2783 * Fixup fid's pointing to the old name to
2784 * start pointing to the new name
2786 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2787 if (vs->fidp == fidp) {
2789 * we replace name of this fid towards the end
2790 * so that our below strcmp will work
2792 continue;
2794 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2795 strlen(vs->fidp->path.data))) {
2796 /* replace the name */
2797 v9fs_fix_path(&fidp->path, &vs->name,
2798 strlen(vs->fidp->path.data));
2801 v9fs_string_copy(&vs->fidp->path, &vs->name);
2804 out:
2805 v9fs_string_free(&vs->name);
2806 return err;
2809 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2811 complete_pdu(s, vs->pdu, err);
2812 qemu_free(vs);
2815 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2817 if (err < 0) {
2818 goto out;
2821 if (vs->v9stat.name.size != 0) {
2822 V9fsRenameState *vr;
2824 vr = qemu_mallocz(sizeof(V9fsRenameState));
2825 vr->newdirfid = -1;
2826 vr->pdu = vs->pdu;
2827 vr->fidp = vs->fidp;
2828 vr->offset = vs->offset;
2829 vr->name.size = vs->v9stat.name.size;
2830 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2832 err = v9fs_complete_rename(s, vr);
2833 qemu_free(vr);
2835 v9fs_wstat_post_rename(s, vs, err);
2836 return;
2838 out:
2839 v9fs_stat_free(&vs->v9stat);
2840 complete_pdu(s, vs->pdu, err);
2841 qemu_free(vs);
2844 static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2846 int32_t fid;
2847 V9fsRenameState *vs;
2848 ssize_t err = 0;
2850 vs = qemu_malloc(sizeof(*vs));
2851 vs->pdu = pdu;
2852 vs->offset = 7;
2854 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2856 vs->fidp = lookup_fid(s, fid);
2857 if (vs->fidp == NULL) {
2858 err = -ENOENT;
2859 goto out;
2862 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2864 err = v9fs_complete_rename(s, vs);
2865 v9fs_rename_post_rename(s, vs, err);
2866 return;
2867 out:
2868 complete_pdu(s, vs->pdu, err);
2869 qemu_free(vs);
2872 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2874 if (err < 0) {
2875 goto out;
2878 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2879 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2880 vs->v9stat.n_gid)) {
2881 err = -errno;
2884 v9fs_wstat_post_chown(s, vs, err);
2885 return;
2887 out:
2888 v9fs_stat_free(&vs->v9stat);
2889 complete_pdu(s, vs->pdu, err);
2890 qemu_free(vs);
2893 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2895 if (err < 0) {
2896 goto out;
2899 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2900 struct timespec times[2];
2901 if (vs->v9stat.atime != -1) {
2902 times[0].tv_sec = vs->v9stat.atime;
2903 times[0].tv_nsec = 0;
2904 } else {
2905 times[0].tv_nsec = UTIME_OMIT;
2907 if (vs->v9stat.mtime != -1) {
2908 times[1].tv_sec = vs->v9stat.mtime;
2909 times[1].tv_nsec = 0;
2910 } else {
2911 times[1].tv_nsec = UTIME_OMIT;
2914 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2915 err = -errno;
2919 v9fs_wstat_post_utime(s, vs, err);
2920 return;
2922 out:
2923 v9fs_stat_free(&vs->v9stat);
2924 complete_pdu(s, vs->pdu, err);
2925 qemu_free(vs);
2928 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2930 if (err == -1) {
2931 err = -errno;
2933 v9fs_stat_free(&vs->v9stat);
2934 complete_pdu(s, vs->pdu, err);
2935 qemu_free(vs);
2938 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2940 uint32_t v9_mode;
2942 if (err == -1) {
2943 err = -errno;
2944 goto out;
2947 v9_mode = stat_to_v9mode(&vs->stbuf);
2949 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2950 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2951 /* Attempting to change the type */
2952 err = -EIO;
2953 goto out;
2956 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2957 &vs->v9stat.extension))) {
2958 err = -errno;
2960 v9fs_wstat_post_chmod(s, vs, err);
2961 return;
2963 out:
2964 v9fs_stat_free(&vs->v9stat);
2965 complete_pdu(s, vs->pdu, err);
2966 qemu_free(vs);
2969 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2971 int32_t fid;
2972 V9fsWstatState *vs;
2973 int err = 0;
2975 vs = qemu_malloc(sizeof(*vs));
2976 vs->pdu = pdu;
2977 vs->offset = 7;
2979 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2981 vs->fidp = lookup_fid(s, fid);
2982 if (vs->fidp == NULL) {
2983 err = -EINVAL;
2984 goto out;
2987 /* do we need to sync the file? */
2988 if (donttouch_stat(&vs->v9stat)) {
2989 err = v9fs_do_fsync(s, vs->fidp->fs.fd);
2990 v9fs_wstat_post_fsync(s, vs, err);
2991 return;
2994 if (vs->v9stat.mode != -1) {
2995 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2996 v9fs_wstat_post_lstat(s, vs, err);
2997 return;
3000 v9fs_wstat_post_chmod(s, vs, err);
3001 return;
3003 out:
3004 v9fs_stat_free(&vs->v9stat);
3005 complete_pdu(s, vs->pdu, err);
3006 qemu_free(vs);
3009 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3011 int32_t bsize_factor;
3013 if (err) {
3014 err = -errno;
3015 goto out;
3019 * compute bsize factor based on host file system block size
3020 * and client msize
3022 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3023 if (!bsize_factor) {
3024 bsize_factor = 1;
3026 vs->v9statfs.f_type = vs->stbuf.f_type;
3027 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3028 vs->v9statfs.f_bsize *= bsize_factor;
3030 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3031 * adjust(divide) the number of blocks, free blocks and available
3032 * blocks by bsize factor
3034 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3035 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3036 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3037 vs->v9statfs.f_files = vs->stbuf.f_files;
3038 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3039 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3040 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3041 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3043 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3044 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3045 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3046 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3047 vs->v9statfs.f_namelen);
3049 out:
3050 complete_pdu(s, vs->pdu, vs->offset);
3051 qemu_free(vs);
3054 static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3056 V9fsStatfsState *vs;
3057 ssize_t err = 0;
3059 vs = qemu_malloc(sizeof(*vs));
3060 vs->pdu = pdu;
3061 vs->offset = 7;
3063 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3065 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3067 vs->fidp = lookup_fid(s, vs->fid);
3068 if (vs->fidp == NULL) {
3069 err = -ENOENT;
3070 goto out;
3073 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3074 v9fs_statfs_post_statfs(s, vs, err);
3075 return;
3077 out:
3078 complete_pdu(s, vs->pdu, err);
3079 qemu_free(vs);
3082 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3084 if (err == -1) {
3085 err = -errno;
3086 goto out;
3089 stat_to_qid(&vs->stbuf, &vs->qid);
3090 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3091 err = vs->offset;
3092 out:
3093 complete_pdu(s, vs->pdu, err);
3094 v9fs_string_free(&vs->fullname);
3095 v9fs_string_free(&vs->name);
3096 qemu_free(vs);
3099 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3101 if (err == -1) {
3102 err = -errno;
3103 goto out;
3106 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3107 v9fs_mknod_post_lstat(s, vs, err);
3108 return;
3109 out:
3110 complete_pdu(s, vs->pdu, err);
3111 v9fs_string_free(&vs->fullname);
3112 v9fs_string_free(&vs->name);
3113 qemu_free(vs);
3116 static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3118 int32_t fid;
3119 V9fsMkState *vs;
3120 int err = 0;
3121 V9fsFidState *fidp;
3122 gid_t gid;
3123 int mode;
3124 int major, minor;
3126 vs = qemu_malloc(sizeof(*vs));
3127 vs->pdu = pdu;
3128 vs->offset = 7;
3130 v9fs_string_init(&vs->fullname);
3131 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3132 &major, &minor, &gid);
3134 fidp = lookup_fid(s, fid);
3135 if (fidp == NULL) {
3136 err = -ENOENT;
3137 goto out;
3140 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3141 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3142 fidp->uid, gid);
3143 v9fs_mknod_post_mknod(s, vs, err);
3144 return;
3146 out:
3147 complete_pdu(s, vs->pdu, err);
3148 v9fs_string_free(&vs->fullname);
3149 v9fs_string_free(&vs->name);
3150 qemu_free(vs);
3153 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3155 if (err == -1) {
3156 err = -errno;
3157 goto out;
3160 stat_to_qid(&vs->stbuf, &vs->qid);
3161 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3162 err = vs->offset;
3163 out:
3164 complete_pdu(s, vs->pdu, err);
3165 v9fs_string_free(&vs->fullname);
3166 v9fs_string_free(&vs->name);
3167 qemu_free(vs);
3170 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3172 if (err == -1) {
3173 err = -errno;
3174 goto out;
3177 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3178 v9fs_mkdir_post_lstat(s, vs, err);
3179 return;
3180 out:
3181 complete_pdu(s, vs->pdu, err);
3182 v9fs_string_free(&vs->fullname);
3183 v9fs_string_free(&vs->name);
3184 qemu_free(vs);
3187 static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3189 int32_t fid;
3190 V9fsMkState *vs;
3191 int err = 0;
3192 V9fsFidState *fidp;
3193 gid_t gid;
3194 int mode;
3196 vs = qemu_malloc(sizeof(*vs));
3197 vs->pdu = pdu;
3198 vs->offset = 7;
3200 v9fs_string_init(&vs->fullname);
3201 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3202 &gid);
3204 fidp = lookup_fid(s, fid);
3205 if (fidp == NULL) {
3206 err = -ENOENT;
3207 goto out;
3210 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3211 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3212 v9fs_mkdir_post_mkdir(s, vs, err);
3213 return;
3215 out:
3216 complete_pdu(s, vs->pdu, err);
3217 v9fs_string_free(&vs->fullname);
3218 v9fs_string_free(&vs->name);
3219 qemu_free(vs);
3222 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3225 if (err < 0) {
3226 err = -errno;
3227 free_fid(s, vs->xattr_fidp->fid);
3228 goto out;
3230 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3231 err = vs->offset;
3232 out:
3233 complete_pdu(s, vs->pdu, err);
3234 v9fs_string_free(&vs->name);
3235 qemu_free(vs);
3236 return;
3239 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3241 if (err < 0) {
3242 err = -errno;
3243 free_fid(s, vs->xattr_fidp->fid);
3244 goto out;
3247 * Read the xattr value
3249 vs->xattr_fidp->fs.xattr.len = vs->size;
3250 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3251 vs->xattr_fidp->fs.xattr.copied_len = -1;
3252 if (vs->size) {
3253 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3254 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3255 &vs->name, vs->xattr_fidp->fs.xattr.value,
3256 vs->xattr_fidp->fs.xattr.len);
3258 v9fs_post_xattr_getvalue(s, vs, err);
3259 return;
3260 out:
3261 complete_pdu(s, vs->pdu, err);
3262 v9fs_string_free(&vs->name);
3263 qemu_free(vs);
3266 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3267 V9fsXattrState *vs, int err)
3269 if (err < 0) {
3270 err = -errno;
3271 free_fid(s, vs->xattr_fidp->fid);
3272 goto out;
3274 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3275 err = vs->offset;
3276 out:
3277 complete_pdu(s, vs->pdu, err);
3278 v9fs_string_free(&vs->name);
3279 qemu_free(vs);
3280 return;
3283 static void v9fs_post_lxattr_check(V9fsState *s,
3284 V9fsXattrState *vs, ssize_t err)
3286 if (err < 0) {
3287 err = -errno;
3288 free_fid(s, vs->xattr_fidp->fid);
3289 goto out;
3292 * Read the xattr value
3294 vs->xattr_fidp->fs.xattr.len = vs->size;
3295 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3296 vs->xattr_fidp->fs.xattr.copied_len = -1;
3297 if (vs->size) {
3298 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3299 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3300 vs->xattr_fidp->fs.xattr.value,
3301 vs->xattr_fidp->fs.xattr.len);
3303 v9fs_post_lxattr_getvalue(s, vs, err);
3304 return;
3305 out:
3306 complete_pdu(s, vs->pdu, err);
3307 v9fs_string_free(&vs->name);
3308 qemu_free(vs);
3311 static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3313 ssize_t err = 0;
3314 V9fsXattrState *vs;
3315 int32_t fid, newfid;
3317 vs = qemu_malloc(sizeof(*vs));
3318 vs->pdu = pdu;
3319 vs->offset = 7;
3321 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3322 vs->file_fidp = lookup_fid(s, fid);
3323 if (vs->file_fidp == NULL) {
3324 err = -ENOENT;
3325 goto out;
3328 vs->xattr_fidp = alloc_fid(s, newfid);
3329 if (vs->xattr_fidp == NULL) {
3330 err = -EINVAL;
3331 goto out;
3334 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3335 if (vs->name.data[0] == 0) {
3337 * listxattr request. Get the size first
3339 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3340 NULL, 0);
3341 if (vs->size < 0) {
3342 err = vs->size;
3344 v9fs_post_lxattr_check(s, vs, err);
3345 return;
3346 } else {
3348 * specific xattr fid. We check for xattr
3349 * presence also collect the xattr size
3351 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3352 &vs->name, NULL, 0);
3353 if (vs->size < 0) {
3354 err = vs->size;
3356 v9fs_post_xattr_check(s, vs, err);
3357 return;
3359 out:
3360 complete_pdu(s, vs->pdu, err);
3361 v9fs_string_free(&vs->name);
3362 qemu_free(vs);
3365 static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3367 int flags;
3368 int32_t fid;
3369 ssize_t err = 0;
3370 V9fsXattrState *vs;
3372 vs = qemu_malloc(sizeof(*vs));
3373 vs->pdu = pdu;
3374 vs->offset = 7;
3376 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3377 &fid, &vs->name, &vs->size, &flags);
3379 vs->file_fidp = lookup_fid(s, fid);
3380 if (vs->file_fidp == NULL) {
3381 err = -EINVAL;
3382 goto out;
3385 /* Make the file fid point to xattr */
3386 vs->xattr_fidp = vs->file_fidp;
3387 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3388 vs->xattr_fidp->fs.xattr.copied_len = 0;
3389 vs->xattr_fidp->fs.xattr.len = vs->size;
3390 vs->xattr_fidp->fs.xattr.flags = flags;
3391 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3392 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3393 if (vs->size)
3394 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3395 else
3396 vs->xattr_fidp->fs.xattr.value = NULL;
3398 out:
3399 complete_pdu(s, vs->pdu, err);
3400 v9fs_string_free(&vs->name);
3401 qemu_free(vs);
3404 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3406 static pdu_handler_t *pdu_handlers[] = {
3407 [P9_TREADDIR] = v9fs_readdir,
3408 [P9_TSTATFS] = v9fs_statfs,
3409 [P9_TGETATTR] = v9fs_getattr,
3410 [P9_TSETATTR] = v9fs_setattr,
3411 [P9_TXATTRWALK] = v9fs_xattrwalk,
3412 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3413 [P9_TMKNOD] = v9fs_mknod,
3414 [P9_TRENAME] = v9fs_rename,
3415 [P9_TMKDIR] = v9fs_mkdir,
3416 [P9_TVERSION] = v9fs_version,
3417 [P9_TLOPEN] = v9fs_open,
3418 [P9_TATTACH] = v9fs_attach,
3419 [P9_TSTAT] = v9fs_stat,
3420 [P9_TWALK] = v9fs_walk,
3421 [P9_TCLUNK] = v9fs_clunk,
3422 [P9_TOPEN] = v9fs_open,
3423 [P9_TREAD] = v9fs_read,
3424 #if 0
3425 [P9_TAUTH] = v9fs_auth,
3426 #endif
3427 [P9_TFLUSH] = v9fs_flush,
3428 [P9_TLINK] = v9fs_link,
3429 [P9_TSYMLINK] = v9fs_symlink,
3430 [P9_TCREATE] = v9fs_create,
3431 [P9_TLCREATE] = v9fs_lcreate,
3432 [P9_TWRITE] = v9fs_write,
3433 [P9_TWSTAT] = v9fs_wstat,
3434 [P9_TREMOVE] = v9fs_remove,
3437 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3439 pdu_handler_t *handler;
3441 if (debug_9p_pdu) {
3442 pprint_pdu(pdu);
3445 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
3447 handler = pdu_handlers[pdu->id];
3448 BUG_ON(handler == NULL);
3450 handler(s, pdu);
3453 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3455 V9fsState *s = (V9fsState *)vdev;
3456 V9fsPDU *pdu;
3457 ssize_t len;
3459 while ((pdu = alloc_pdu(s)) &&
3460 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3461 uint8_t *ptr;
3463 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3464 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3466 ptr = pdu->elem.out_sg[0].iov_base;
3468 memcpy(&pdu->size, ptr, 4);
3469 pdu->id = ptr[4];
3470 memcpy(&pdu->tag, ptr + 5, 2);
3472 submit_pdu(s, pdu);
3475 free_pdu(s, pdu);
3478 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3480 features |= 1 << VIRTIO_9P_MOUNT_TAG;
3481 return features;
3484 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3486 return (V9fsState *)vdev;
3489 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
3491 struct virtio_9p_config *cfg;
3492 V9fsState *s = to_virtio_9p(vdev);
3494 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
3495 s->tag_len);
3496 stw_raw(&cfg->tag_len, s->tag_len);
3497 memcpy(cfg->tag, s->tag, s->tag_len);
3498 memcpy(config, cfg, s->config_size);
3499 qemu_free(cfg);
3502 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
3504 V9fsState *s;
3505 int i, len;
3506 struct stat stat;
3507 FsTypeEntry *fse;
3510 s = (V9fsState *)virtio_common_init("virtio-9p",
3511 VIRTIO_ID_9P,
3512 sizeof(struct virtio_9p_config)+
3513 MAX_TAG_LEN,
3514 sizeof(V9fsState));
3516 /* initialize pdu allocator */
3517 QLIST_INIT(&s->free_list);
3518 for (i = 0; i < (MAX_REQ - 1); i++) {
3519 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3522 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
3524 fse = get_fsdev_fsentry(conf->fsdev_id);
3526 if (!fse) {
3527 /* We don't have a fsdev identified by fsdev_id */
3528 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
3529 "with the id %s\n", conf->fsdev_id);
3530 exit(1);
3533 if (!fse->path || !conf->tag) {
3534 /* we haven't specified a mount_tag or the path */
3535 fprintf(stderr, "fsdev with id %s needs path "
3536 "and Virtio-9p device needs mount_tag arguments\n",
3537 conf->fsdev_id);
3538 exit(1);
3541 if (!strcmp(fse->security_model, "passthrough")) {
3542 /* Files on the Fileserver set to client user credentials */
3543 s->ctx.fs_sm = SM_PASSTHROUGH;
3544 } else if (!strcmp(fse->security_model, "mapped")) {
3545 /* Files on the fileserver are set to QEMU credentials.
3546 * Client user credentials are saved in extended attributes.
3548 s->ctx.fs_sm = SM_MAPPED;
3549 } else if (!strcmp(fse->security_model, "none")) {
3551 * Files on the fileserver are set to QEMU credentials.
3553 s->ctx.fs_sm = SM_NONE;
3555 } else {
3556 fprintf(stderr, "Default to security_model=none. You may want"
3557 " enable advanced security model using "
3558 "security option:\n\t security_model=passthrough \n\t "
3559 "security_model=mapped\n");
3560 s->ctx.fs_sm = SM_NONE;
3563 if (lstat(fse->path, &stat)) {
3564 fprintf(stderr, "share path %s does not exist\n", fse->path);
3565 exit(1);
3566 } else if (!S_ISDIR(stat.st_mode)) {
3567 fprintf(stderr, "share path %s is not a directory \n", fse->path);
3568 exit(1);
3571 s->ctx.fs_root = qemu_strdup(fse->path);
3572 len = strlen(conf->tag);
3573 if (len > MAX_TAG_LEN) {
3574 len = MAX_TAG_LEN;
3576 /* s->tag is non-NULL terminated string */
3577 s->tag = qemu_malloc(len);
3578 memcpy(s->tag, conf->tag, len);
3579 s->tag_len = len;
3580 s->ctx.uid = -1;
3582 s->ops = fse->ops;
3583 s->vdev.get_features = virtio_9p_get_features;
3584 s->config_size = sizeof(struct virtio_9p_config) +
3585 s->tag_len;
3586 s->vdev.get_config = virtio_9p_get_config;
3588 return &s->vdev;