virtio-9p: Add support for removing xattr
[qemu.git] / hw / virtio-9p.c
blobeb7ae01af54773dd3bd8425b666b403ca1a68f98
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 dotu = 1;
22 int debug_9p_pdu;
24 enum {
25 Oread = 0x00,
26 Owrite = 0x01,
27 Ordwr = 0x02,
28 Oexec = 0x03,
29 Oexcl = 0x04,
30 Otrunc = 0x10,
31 Orexec = 0x20,
32 Orclose = 0x40,
33 Oappend = 0x80,
36 static int omode_to_uflags(int8_t mode)
38 int ret = 0;
40 switch (mode & 3) {
41 case Oread:
42 ret = O_RDONLY;
43 break;
44 case Ordwr:
45 ret = O_RDWR;
46 break;
47 case Owrite:
48 ret = O_WRONLY;
49 break;
50 case Oexec:
51 ret = O_RDONLY;
52 break;
55 if (mode & Otrunc) {
56 ret |= O_TRUNC;
59 if (mode & Oappend) {
60 ret |= O_APPEND;
63 if (mode & Oexcl) {
64 ret |= O_EXCL;
67 return ret;
70 void cred_init(FsCred *credp)
72 credp->fc_uid = -1;
73 credp->fc_gid = -1;
74 credp->fc_mode = -1;
75 credp->fc_rdev = -1;
78 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
80 return s->ops->lstat(&s->ctx, path->data, stbuf);
83 static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
85 ssize_t len;
87 buf->data = qemu_malloc(1024);
89 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
90 if (len > -1) {
91 buf->size = len;
92 buf->data[len] = 0;
95 return len;
98 static int v9fs_do_close(V9fsState *s, int fd)
100 return s->ops->close(&s->ctx, fd);
103 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
105 return s->ops->closedir(&s->ctx, dir);
108 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
110 return s->ops->open(&s->ctx, path->data, flags);
113 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
115 return s->ops->opendir(&s->ctx, path->data);
118 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
120 return s->ops->rewinddir(&s->ctx, dir);
123 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
125 return s->ops->telldir(&s->ctx, dir);
128 static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
130 return s->ops->readdir(&s->ctx, dir);
133 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
135 return s->ops->seekdir(&s->ctx, dir, off);
138 static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
139 int iovcnt)
141 return s->ops->readv(&s->ctx, fd, iov, iovcnt);
144 static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
146 return s->ops->lseek(&s->ctx, fd, offset, whence);
149 static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
150 int iovcnt)
152 return s->ops->writev(&s->ctx, fd, iov, iovcnt);
155 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
157 FsCred cred;
158 cred_init(&cred);
159 cred.fc_mode = mode;
160 return s->ops->chmod(&s->ctx, path->data, &cred);
163 static int v9fs_do_mknod(V9fsState *s, char *name,
164 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
166 FsCred cred;
167 cred_init(&cred);
168 cred.fc_uid = uid;
169 cred.fc_gid = gid;
170 cred.fc_mode = mode;
171 cred.fc_rdev = dev;
172 return s->ops->mknod(&s->ctx, name, &cred);
175 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
176 uid_t uid, gid_t gid)
178 FsCred cred;
180 cred_init(&cred);
181 cred.fc_uid = uid;
182 cred.fc_gid = gid;
183 cred.fc_mode = mode;
185 return s->ops->mkdir(&s->ctx, name, &cred);
188 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
190 return s->ops->fstat(&s->ctx, fd, stbuf);
193 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
194 int flags, int mode)
196 FsCred cred;
198 cred_init(&cred);
199 cred.fc_uid = uid;
200 cred.fc_gid = gid;
201 cred.fc_mode = mode & 07777;
202 flags = flags;
204 return s->ops->open2(&s->ctx, fullname, flags, &cred);
207 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
208 const char *oldpath, const char *newpath, gid_t gid)
210 FsCred cred;
211 cred_init(&cred);
212 cred.fc_uid = fidp->uid;
213 cred.fc_gid = gid;
214 cred.fc_mode = 0777;
216 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
219 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
221 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
224 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
226 return s->ops->truncate(&s->ctx, path->data, size);
229 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
230 V9fsString *newpath)
232 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
235 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
237 FsCred cred;
238 cred_init(&cred);
239 cred.fc_uid = uid;
240 cred.fc_gid = gid;
242 return s->ops->chown(&s->ctx, path->data, &cred);
245 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
246 const struct timespec times[2])
248 return s->ops->utimensat(&s->ctx, path->data, times);
251 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
253 return s->ops->remove(&s->ctx, path->data);
256 static int v9fs_do_fsync(V9fsState *s, int fd)
258 return s->ops->fsync(&s->ctx, fd);
261 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
263 return s->ops->statfs(&s->ctx, path->data, stbuf);
266 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
267 V9fsString *xattr_name,
268 void *value, size_t size)
270 return s->ops->lgetxattr(&s->ctx, path->data,
271 xattr_name->data, value, size);
274 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
275 void *value, size_t size)
277 return s->ops->llistxattr(&s->ctx, path->data,
278 value, size);
281 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
282 V9fsString *xattr_name,
283 void *value, size_t size, int flags)
285 return s->ops->lsetxattr(&s->ctx, path->data,
286 xattr_name->data, value, size, flags);
289 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
290 V9fsString *xattr_name)
292 return s->ops->lremovexattr(&s->ctx, path->data,
293 xattr_name->data);
297 static void v9fs_string_init(V9fsString *str)
299 str->data = NULL;
300 str->size = 0;
303 static void v9fs_string_free(V9fsString *str)
305 qemu_free(str->data);
306 str->data = NULL;
307 str->size = 0;
310 static void v9fs_string_null(V9fsString *str)
312 v9fs_string_free(str);
315 static int number_to_string(void *arg, char type)
317 unsigned int ret = 0;
319 switch (type) {
320 case 'u': {
321 unsigned int num = *(unsigned int *)arg;
323 do {
324 ret++;
325 num = num/10;
326 } while (num);
327 break;
329 default:
330 printf("Number_to_string: Unknown number format\n");
331 return -1;
334 return ret;
337 static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
339 va_list ap2;
340 char *iter = (char *)fmt;
341 int len = 0;
342 int nr_args = 0;
343 char *arg_char_ptr;
344 unsigned int arg_uint;
346 /* Find the number of %'s that denotes an argument */
347 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
348 nr_args++;
349 iter++;
352 len = strlen(fmt) - 2*nr_args;
354 if (!nr_args) {
355 goto alloc_print;
358 va_copy(ap2, ap);
360 iter = (char *)fmt;
362 /* Now parse the format string */
363 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
364 iter++;
365 switch (*iter) {
366 case 'u':
367 arg_uint = va_arg(ap2, unsigned int);
368 len += number_to_string((void *)&arg_uint, 'u');
369 break;
370 case 's':
371 arg_char_ptr = va_arg(ap2, char *);
372 len += strlen(arg_char_ptr);
373 break;
374 case 'c':
375 len += 1;
376 break;
377 default:
378 fprintf(stderr,
379 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
380 return -1;
382 iter++;
385 alloc_print:
386 *strp = qemu_malloc((len + 1) * sizeof(**strp));
388 return vsprintf(*strp, fmt, ap);
391 static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
393 va_list ap;
394 int err;
396 v9fs_string_free(str);
398 va_start(ap, fmt);
399 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
400 BUG_ON(err == -1);
401 va_end(ap);
403 str->size = err;
406 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
408 v9fs_string_free(lhs);
409 v9fs_string_sprintf(lhs, "%s", rhs->data);
412 static size_t v9fs_string_size(V9fsString *str)
414 return str->size;
417 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
419 V9fsFidState *f;
421 for (f = s->fid_list; f; f = f->next) {
422 if (f->fid == fid) {
423 return f;
427 return NULL;
430 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
432 V9fsFidState *f;
434 f = lookup_fid(s, fid);
435 if (f) {
436 return NULL;
439 f = qemu_mallocz(sizeof(V9fsFidState));
441 f->fid = fid;
442 f->fid_type = P9_FID_NONE;
444 f->next = s->fid_list;
445 s->fid_list = f;
447 return f;
450 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
452 int retval = 0;
454 if (fidp->fs.xattr.copied_len == -1) {
455 /* getxattr/listxattr fid */
456 goto free_value;
459 * if this is fid for setxattr. clunk should
460 * result in setxattr localcall
462 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
463 /* clunk after partial write */
464 retval = -EINVAL;
465 goto free_out;
467 if (fidp->fs.xattr.len) {
468 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
469 fidp->fs.xattr.value,
470 fidp->fs.xattr.len,
471 fidp->fs.xattr.flags);
472 } else {
473 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
475 free_out:
476 v9fs_string_free(&fidp->fs.xattr.name);
477 free_value:
478 if (fidp->fs.xattr.value) {
479 qemu_free(fidp->fs.xattr.value);
481 return retval;
484 static int free_fid(V9fsState *s, int32_t fid)
486 int retval = 0;
487 V9fsFidState **fidpp, *fidp;
489 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
490 if ((*fidpp)->fid == fid) {
491 break;
495 if (*fidpp == NULL) {
496 return -ENOENT;
499 fidp = *fidpp;
500 *fidpp = fidp->next;
502 if (fidp->fid_type == P9_FID_FILE) {
503 v9fs_do_close(s, fidp->fs.fd);
504 } else if (fidp->fid_type == P9_FID_DIR) {
505 v9fs_do_closedir(s, fidp->fs.dir);
506 } else if (fidp->fid_type == P9_FID_XATTR) {
507 retval = v9fs_xattr_fid_clunk(s, fidp);
509 v9fs_string_free(&fidp->path);
510 qemu_free(fidp);
512 return retval;
515 #define P9_QID_TYPE_DIR 0x80
516 #define P9_QID_TYPE_SYMLINK 0x02
518 #define P9_STAT_MODE_DIR 0x80000000
519 #define P9_STAT_MODE_APPEND 0x40000000
520 #define P9_STAT_MODE_EXCL 0x20000000
521 #define P9_STAT_MODE_MOUNT 0x10000000
522 #define P9_STAT_MODE_AUTH 0x08000000
523 #define P9_STAT_MODE_TMP 0x04000000
524 #define P9_STAT_MODE_SYMLINK 0x02000000
525 #define P9_STAT_MODE_LINK 0x01000000
526 #define P9_STAT_MODE_DEVICE 0x00800000
527 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
528 #define P9_STAT_MODE_SOCKET 0x00100000
529 #define P9_STAT_MODE_SETUID 0x00080000
530 #define P9_STAT_MODE_SETGID 0x00040000
531 #define P9_STAT_MODE_SETVTX 0x00010000
533 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
534 P9_STAT_MODE_SYMLINK | \
535 P9_STAT_MODE_LINK | \
536 P9_STAT_MODE_DEVICE | \
537 P9_STAT_MODE_NAMED_PIPE | \
538 P9_STAT_MODE_SOCKET)
540 /* This is the algorithm from ufs in spfs */
541 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
543 size_t size;
545 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
546 memcpy(&qidp->path, &stbuf->st_ino, size);
547 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
548 qidp->type = 0;
549 if (S_ISDIR(stbuf->st_mode)) {
550 qidp->type |= P9_QID_TYPE_DIR;
552 if (S_ISLNK(stbuf->st_mode)) {
553 qidp->type |= P9_QID_TYPE_SYMLINK;
557 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
559 struct stat stbuf;
560 int err;
562 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
563 if (err) {
564 return err;
567 stat_to_qid(&stbuf, qidp);
568 return 0;
571 static V9fsPDU *alloc_pdu(V9fsState *s)
573 V9fsPDU *pdu = NULL;
575 if (!QLIST_EMPTY(&s->free_list)) {
576 pdu = QLIST_FIRST(&s->free_list);
577 QLIST_REMOVE(pdu, next);
579 return pdu;
582 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
584 if (pdu) {
585 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
589 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
590 size_t offset, size_t size, int pack)
592 int i = 0;
593 size_t copied = 0;
595 for (i = 0; size && i < sg_count; i++) {
596 size_t len;
597 if (offset >= sg[i].iov_len) {
598 /* skip this sg */
599 offset -= sg[i].iov_len;
600 continue;
601 } else {
602 len = MIN(sg[i].iov_len - offset, size);
603 if (pack) {
604 memcpy(sg[i].iov_base + offset, addr, len);
605 } else {
606 memcpy(addr, sg[i].iov_base + offset, len);
608 size -= len;
609 copied += len;
610 addr += len;
611 if (size) {
612 offset = 0;
613 continue;
618 return copied;
621 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
623 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
624 offset, size, 0);
627 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
628 size_t size)
630 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
631 offset, size, 1);
634 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
636 size_t pos = 0;
637 int i, j;
638 struct iovec *src_sg;
639 unsigned int num;
641 if (rx) {
642 src_sg = pdu->elem.in_sg;
643 num = pdu->elem.in_num;
644 } else {
645 src_sg = pdu->elem.out_sg;
646 num = pdu->elem.out_num;
649 j = 0;
650 for (i = 0; i < num; i++) {
651 if (offset <= pos) {
652 sg[j].iov_base = src_sg[i].iov_base;
653 sg[j].iov_len = src_sg[i].iov_len;
654 j++;
655 } else if (offset < (src_sg[i].iov_len + pos)) {
656 sg[j].iov_base = src_sg[i].iov_base;
657 sg[j].iov_len = src_sg[i].iov_len;
658 sg[j].iov_base += (offset - pos);
659 sg[j].iov_len -= (offset - pos);
660 j++;
662 pos += src_sg[i].iov_len;
665 return j;
668 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
670 size_t old_offset = offset;
671 va_list ap;
672 int i;
674 va_start(ap, fmt);
675 for (i = 0; fmt[i]; i++) {
676 switch (fmt[i]) {
677 case 'b': {
678 uint8_t *valp = va_arg(ap, uint8_t *);
679 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
680 break;
682 case 'w': {
683 uint16_t val, *valp;
684 valp = va_arg(ap, uint16_t *);
685 val = le16_to_cpupu(valp);
686 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
687 *valp = val;
688 break;
690 case 'd': {
691 uint32_t val, *valp;
692 valp = va_arg(ap, uint32_t *);
693 val = le32_to_cpupu(valp);
694 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
695 *valp = val;
696 break;
698 case 'q': {
699 uint64_t val, *valp;
700 valp = va_arg(ap, uint64_t *);
701 val = le64_to_cpup(valp);
702 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
703 *valp = val;
704 break;
706 case 'v': {
707 struct iovec *iov = va_arg(ap, struct iovec *);
708 int *iovcnt = va_arg(ap, int *);
709 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
710 break;
712 case 's': {
713 V9fsString *str = va_arg(ap, V9fsString *);
714 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
715 /* FIXME: sanity check str->size */
716 str->data = qemu_malloc(str->size + 1);
717 offset += pdu_unpack(str->data, pdu, offset, str->size);
718 str->data[str->size] = 0;
719 break;
721 case 'Q': {
722 V9fsQID *qidp = va_arg(ap, V9fsQID *);
723 offset += pdu_unmarshal(pdu, offset, "bdq",
724 &qidp->type, &qidp->version, &qidp->path);
725 break;
727 case 'S': {
728 V9fsStat *statp = va_arg(ap, V9fsStat *);
729 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
730 &statp->size, &statp->type, &statp->dev,
731 &statp->qid, &statp->mode, &statp->atime,
732 &statp->mtime, &statp->length,
733 &statp->name, &statp->uid, &statp->gid,
734 &statp->muid, &statp->extension,
735 &statp->n_uid, &statp->n_gid,
736 &statp->n_muid);
737 break;
739 case 'I': {
740 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
741 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
742 &iattr->valid, &iattr->mode,
743 &iattr->uid, &iattr->gid, &iattr->size,
744 &iattr->atime_sec, &iattr->atime_nsec,
745 &iattr->mtime_sec, &iattr->mtime_nsec);
746 break;
748 default:
749 break;
753 va_end(ap);
755 return offset - old_offset;
758 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
760 size_t old_offset = offset;
761 va_list ap;
762 int i;
764 va_start(ap, fmt);
765 for (i = 0; fmt[i]; i++) {
766 switch (fmt[i]) {
767 case 'b': {
768 uint8_t val = va_arg(ap, int);
769 offset += pdu_pack(pdu, offset, &val, sizeof(val));
770 break;
772 case 'w': {
773 uint16_t val;
774 cpu_to_le16w(&val, va_arg(ap, int));
775 offset += pdu_pack(pdu, offset, &val, sizeof(val));
776 break;
778 case 'd': {
779 uint32_t val;
780 cpu_to_le32w(&val, va_arg(ap, uint32_t));
781 offset += pdu_pack(pdu, offset, &val, sizeof(val));
782 break;
784 case 'q': {
785 uint64_t val;
786 cpu_to_le64w(&val, va_arg(ap, uint64_t));
787 offset += pdu_pack(pdu, offset, &val, sizeof(val));
788 break;
790 case 'v': {
791 struct iovec *iov = va_arg(ap, struct iovec *);
792 int *iovcnt = va_arg(ap, int *);
793 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
794 break;
796 case 's': {
797 V9fsString *str = va_arg(ap, V9fsString *);
798 offset += pdu_marshal(pdu, offset, "w", str->size);
799 offset += pdu_pack(pdu, offset, str->data, str->size);
800 break;
802 case 'Q': {
803 V9fsQID *qidp = va_arg(ap, V9fsQID *);
804 offset += pdu_marshal(pdu, offset, "bdq",
805 qidp->type, qidp->version, qidp->path);
806 break;
808 case 'S': {
809 V9fsStat *statp = va_arg(ap, V9fsStat *);
810 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
811 statp->size, statp->type, statp->dev,
812 &statp->qid, statp->mode, statp->atime,
813 statp->mtime, statp->length, &statp->name,
814 &statp->uid, &statp->gid, &statp->muid,
815 &statp->extension, statp->n_uid,
816 statp->n_gid, statp->n_muid);
817 break;
819 case 'A': {
820 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
821 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
822 statp->st_result_mask,
823 &statp->qid, statp->st_mode,
824 statp->st_uid, statp->st_gid,
825 statp->st_nlink, statp->st_rdev,
826 statp->st_size, statp->st_blksize, statp->st_blocks,
827 statp->st_atime_sec, statp->st_atime_nsec,
828 statp->st_mtime_sec, statp->st_mtime_nsec,
829 statp->st_ctime_sec, statp->st_ctime_nsec,
830 statp->st_btime_sec, statp->st_btime_nsec,
831 statp->st_gen, statp->st_data_version);
832 break;
834 default:
835 break;
838 va_end(ap);
840 return offset - old_offset;
843 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
845 int8_t id = pdu->id + 1; /* Response */
847 if (len < 0) {
848 V9fsString str;
849 int err = -len;
851 str.data = strerror(err);
852 str.size = strlen(str.data);
854 len = 7;
855 len += pdu_marshal(pdu, len, "s", &str);
856 if (dotu) {
857 len += pdu_marshal(pdu, len, "d", err);
860 id = P9_RERROR;
863 /* fill out the header */
864 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
866 /* keep these in sync */
867 pdu->size = len;
868 pdu->id = id;
870 /* push onto queue and notify */
871 virtqueue_push(s->vq, &pdu->elem, len);
873 /* FIXME: we should batch these completions */
874 virtio_notify(&s->vdev, s->vq);
876 free_pdu(s, pdu);
879 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
881 mode_t ret;
883 ret = mode & 0777;
884 if (mode & P9_STAT_MODE_DIR) {
885 ret |= S_IFDIR;
888 if (dotu) {
889 if (mode & P9_STAT_MODE_SYMLINK) {
890 ret |= S_IFLNK;
892 if (mode & P9_STAT_MODE_SOCKET) {
893 ret |= S_IFSOCK;
895 if (mode & P9_STAT_MODE_NAMED_PIPE) {
896 ret |= S_IFIFO;
898 if (mode & P9_STAT_MODE_DEVICE) {
899 if (extension && extension->data[0] == 'c') {
900 ret |= S_IFCHR;
901 } else {
902 ret |= S_IFBLK;
907 if (!(ret&~0777)) {
908 ret |= S_IFREG;
911 if (mode & P9_STAT_MODE_SETUID) {
912 ret |= S_ISUID;
914 if (mode & P9_STAT_MODE_SETGID) {
915 ret |= S_ISGID;
917 if (mode & P9_STAT_MODE_SETVTX) {
918 ret |= S_ISVTX;
921 return ret;
924 static int donttouch_stat(V9fsStat *stat)
926 if (stat->type == -1 &&
927 stat->dev == -1 &&
928 stat->qid.type == -1 &&
929 stat->qid.version == -1 &&
930 stat->qid.path == -1 &&
931 stat->mode == -1 &&
932 stat->atime == -1 &&
933 stat->mtime == -1 &&
934 stat->length == -1 &&
935 !stat->name.size &&
936 !stat->uid.size &&
937 !stat->gid.size &&
938 !stat->muid.size &&
939 stat->n_uid == -1 &&
940 stat->n_gid == -1 &&
941 stat->n_muid == -1) {
942 return 1;
945 return 0;
948 static void v9fs_stat_free(V9fsStat *stat)
950 v9fs_string_free(&stat->name);
951 v9fs_string_free(&stat->uid);
952 v9fs_string_free(&stat->gid);
953 v9fs_string_free(&stat->muid);
954 v9fs_string_free(&stat->extension);
957 static uint32_t stat_to_v9mode(const struct stat *stbuf)
959 uint32_t mode;
961 mode = stbuf->st_mode & 0777;
962 if (S_ISDIR(stbuf->st_mode)) {
963 mode |= P9_STAT_MODE_DIR;
966 if (dotu) {
967 if (S_ISLNK(stbuf->st_mode)) {
968 mode |= P9_STAT_MODE_SYMLINK;
971 if (S_ISSOCK(stbuf->st_mode)) {
972 mode |= P9_STAT_MODE_SOCKET;
975 if (S_ISFIFO(stbuf->st_mode)) {
976 mode |= P9_STAT_MODE_NAMED_PIPE;
979 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
980 mode |= P9_STAT_MODE_DEVICE;
983 if (stbuf->st_mode & S_ISUID) {
984 mode |= P9_STAT_MODE_SETUID;
987 if (stbuf->st_mode & S_ISGID) {
988 mode |= P9_STAT_MODE_SETGID;
991 if (stbuf->st_mode & S_ISVTX) {
992 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 if (dotu) {
1019 v9stat->n_uid = stbuf->st_uid;
1020 v9stat->n_gid = stbuf->st_gid;
1021 v9stat->n_muid = 0;
1023 v9fs_string_null(&v9stat->extension);
1025 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1026 err = v9fs_do_readlink(s, name, &v9stat->extension);
1027 if (err == -1) {
1028 err = -errno;
1029 return err;
1031 v9stat->extension.data[err] = 0;
1032 v9stat->extension.size = err;
1033 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1034 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1035 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1036 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1037 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1038 v9fs_string_sprintf(&v9stat->extension, "%s %u",
1039 "HARDLINKCOUNT", stbuf->st_nlink);
1043 str = strrchr(name->data, '/');
1044 if (str) {
1045 str += 1;
1046 } else {
1047 str = name->data;
1050 v9fs_string_sprintf(&v9stat->name, "%s", str);
1052 v9stat->size = 61 +
1053 v9fs_string_size(&v9stat->name) +
1054 v9fs_string_size(&v9stat->uid) +
1055 v9fs_string_size(&v9stat->gid) +
1056 v9fs_string_size(&v9stat->muid) +
1057 v9fs_string_size(&v9stat->extension);
1058 return 0;
1061 #define P9_STATS_MODE 0x00000001ULL
1062 #define P9_STATS_NLINK 0x00000002ULL
1063 #define P9_STATS_UID 0x00000004ULL
1064 #define P9_STATS_GID 0x00000008ULL
1065 #define P9_STATS_RDEV 0x00000010ULL
1066 #define P9_STATS_ATIME 0x00000020ULL
1067 #define P9_STATS_MTIME 0x00000040ULL
1068 #define P9_STATS_CTIME 0x00000080ULL
1069 #define P9_STATS_INO 0x00000100ULL
1070 #define P9_STATS_SIZE 0x00000200ULL
1071 #define P9_STATS_BLOCKS 0x00000400ULL
1073 #define P9_STATS_BTIME 0x00000800ULL
1074 #define P9_STATS_GEN 0x00001000ULL
1075 #define P9_STATS_DATA_VERSION 0x00002000ULL
1077 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1078 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1081 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1082 V9fsStatDotl *v9lstat)
1084 memset(v9lstat, 0, sizeof(*v9lstat));
1086 v9lstat->st_mode = stbuf->st_mode;
1087 v9lstat->st_nlink = stbuf->st_nlink;
1088 v9lstat->st_uid = stbuf->st_uid;
1089 v9lstat->st_gid = stbuf->st_gid;
1090 v9lstat->st_rdev = stbuf->st_rdev;
1091 v9lstat->st_size = stbuf->st_size;
1092 v9lstat->st_blksize = stbuf->st_blksize;
1093 v9lstat->st_blocks = stbuf->st_blocks;
1094 v9lstat->st_atime_sec = stbuf->st_atime;
1095 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1096 v9lstat->st_mtime_sec = stbuf->st_mtime;
1097 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1098 v9lstat->st_ctime_sec = stbuf->st_ctime;
1099 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1100 /* Currently we only support BASIC fields in stat */
1101 v9lstat->st_result_mask = P9_STATS_BASIC;
1103 stat_to_qid(stbuf, &v9lstat->qid);
1106 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1108 while (len && *iovcnt) {
1109 if (len < sg->iov_len) {
1110 sg->iov_len -= len;
1111 sg->iov_base += len;
1112 len = 0;
1113 } else {
1114 len -= sg->iov_len;
1115 sg++;
1116 *iovcnt -= 1;
1120 return sg;
1123 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1125 int i;
1126 int total = 0;
1128 for (i = 0; i < *cnt; i++) {
1129 if ((total + sg[i].iov_len) > cap) {
1130 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1131 i++;
1132 break;
1134 total += sg[i].iov_len;
1137 *cnt = i;
1139 return sg;
1142 static void print_sg(struct iovec *sg, int cnt)
1144 int i;
1146 printf("sg[%d]: {", cnt);
1147 for (i = 0; i < cnt; i++) {
1148 if (i) {
1149 printf(", ");
1151 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1153 printf("}\n");
1156 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1158 V9fsString str;
1159 v9fs_string_init(&str);
1160 v9fs_string_copy(&str, dst);
1161 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1162 v9fs_string_free(&str);
1165 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1167 V9fsString version;
1168 size_t offset = 7;
1170 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1172 if (!strcmp(version.data, "9P2000.u")) {
1173 s->proto_version = V9FS_PROTO_2000U;
1174 } else if (!strcmp(version.data, "9P2000.L")) {
1175 s->proto_version = V9FS_PROTO_2000L;
1176 } else {
1177 v9fs_string_sprintf(&version, "unknown");
1180 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1181 complete_pdu(s, pdu, offset);
1183 v9fs_string_free(&version);
1186 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1188 int32_t fid, afid, n_uname;
1189 V9fsString uname, aname;
1190 V9fsFidState *fidp;
1191 V9fsQID qid;
1192 size_t offset = 7;
1193 ssize_t err;
1195 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1197 fidp = alloc_fid(s, fid);
1198 if (fidp == NULL) {
1199 err = -EINVAL;
1200 goto out;
1203 fidp->uid = n_uname;
1205 v9fs_string_sprintf(&fidp->path, "%s", "/");
1206 err = fid_to_qid(s, fidp, &qid);
1207 if (err) {
1208 err = -EINVAL;
1209 free_fid(s, fid);
1210 goto out;
1213 offset += pdu_marshal(pdu, offset, "Q", &qid);
1215 err = offset;
1216 out:
1217 complete_pdu(s, pdu, err);
1218 v9fs_string_free(&uname);
1219 v9fs_string_free(&aname);
1222 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1224 if (err == -1) {
1225 err = -errno;
1226 goto out;
1229 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1230 if (err) {
1231 goto out;
1233 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1234 err = vs->offset;
1236 out:
1237 complete_pdu(s, vs->pdu, err);
1238 v9fs_stat_free(&vs->v9stat);
1239 qemu_free(vs);
1242 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1244 int32_t fid;
1245 V9fsStatState *vs;
1246 ssize_t err = 0;
1248 vs = qemu_malloc(sizeof(*vs));
1249 vs->pdu = pdu;
1250 vs->offset = 7;
1252 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1254 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1256 vs->fidp = lookup_fid(s, fid);
1257 if (vs->fidp == NULL) {
1258 err = -ENOENT;
1259 goto out;
1262 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1263 v9fs_stat_post_lstat(s, vs, err);
1264 return;
1266 out:
1267 complete_pdu(s, vs->pdu, err);
1268 v9fs_stat_free(&vs->v9stat);
1269 qemu_free(vs);
1272 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1273 int err)
1275 if (err == -1) {
1276 err = -errno;
1277 goto out;
1280 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1281 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1282 err = vs->offset;
1284 out:
1285 complete_pdu(s, vs->pdu, err);
1286 qemu_free(vs);
1289 static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1291 int32_t fid;
1292 V9fsStatStateDotl *vs;
1293 ssize_t err = 0;
1294 V9fsFidState *fidp;
1295 uint64_t request_mask;
1297 vs = qemu_malloc(sizeof(*vs));
1298 vs->pdu = pdu;
1299 vs->offset = 7;
1301 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1303 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1305 fidp = lookup_fid(s, fid);
1306 if (fidp == NULL) {
1307 err = -ENOENT;
1308 goto out;
1311 /* Currently we only support BASIC fields in stat, so there is no
1312 * need to look at request_mask.
1314 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1315 v9fs_getattr_post_lstat(s, vs, err);
1316 return;
1318 out:
1319 complete_pdu(s, vs->pdu, err);
1320 qemu_free(vs);
1323 /* From Linux kernel code */
1324 #define ATTR_MODE (1 << 0)
1325 #define ATTR_UID (1 << 1)
1326 #define ATTR_GID (1 << 2)
1327 #define ATTR_SIZE (1 << 3)
1328 #define ATTR_ATIME (1 << 4)
1329 #define ATTR_MTIME (1 << 5)
1330 #define ATTR_CTIME (1 << 6)
1331 #define ATTR_MASK 127
1332 #define ATTR_ATIME_SET (1 << 7)
1333 #define ATTR_MTIME_SET (1 << 8)
1335 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1336 int err)
1338 if (err == -1) {
1339 err = -errno;
1340 goto out;
1342 err = vs->offset;
1344 out:
1345 complete_pdu(s, vs->pdu, err);
1346 qemu_free(vs);
1349 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1351 if (err == -1) {
1352 err = -errno;
1353 goto out;
1356 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1357 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1359 v9fs_setattr_post_truncate(s, vs, err);
1360 return;
1362 out:
1363 complete_pdu(s, vs->pdu, err);
1364 qemu_free(vs);
1367 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1368 int err)
1370 if (err == -1) {
1371 err = -errno;
1372 goto out;
1375 /* If the only valid entry in iattr is ctime we can call
1376 * chown(-1,-1) to update the ctime of the file
1378 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1379 ((vs->v9iattr.valid & ATTR_CTIME)
1380 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1381 if (!(vs->v9iattr.valid & ATTR_UID)) {
1382 vs->v9iattr.uid = -1;
1384 if (!(vs->v9iattr.valid & ATTR_GID)) {
1385 vs->v9iattr.gid = -1;
1387 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1388 vs->v9iattr.gid);
1390 v9fs_setattr_post_chown(s, vs, err);
1391 return;
1393 out:
1394 complete_pdu(s, vs->pdu, err);
1395 qemu_free(vs);
1398 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1400 if (err == -1) {
1401 err = -errno;
1402 goto out;
1405 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1406 struct timespec times[2];
1407 if (vs->v9iattr.valid & ATTR_ATIME) {
1408 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1409 times[0].tv_sec = vs->v9iattr.atime_sec;
1410 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1411 } else {
1412 times[0].tv_nsec = UTIME_NOW;
1414 } else {
1415 times[0].tv_nsec = UTIME_OMIT;
1418 if (vs->v9iattr.valid & ATTR_MTIME) {
1419 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1420 times[1].tv_sec = vs->v9iattr.mtime_sec;
1421 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1422 } else {
1423 times[1].tv_nsec = UTIME_NOW;
1425 } else {
1426 times[1].tv_nsec = UTIME_OMIT;
1428 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1430 v9fs_setattr_post_utimensat(s, vs, err);
1431 return;
1433 out:
1434 complete_pdu(s, vs->pdu, err);
1435 qemu_free(vs);
1438 static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1440 int32_t fid;
1441 V9fsSetattrState *vs;
1442 int err = 0;
1444 vs = qemu_malloc(sizeof(*vs));
1445 vs->pdu = pdu;
1446 vs->offset = 7;
1448 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1450 vs->fidp = lookup_fid(s, fid);
1451 if (vs->fidp == NULL) {
1452 err = -EINVAL;
1453 goto out;
1456 if (vs->v9iattr.valid & ATTR_MODE) {
1457 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1460 v9fs_setattr_post_chmod(s, vs, err);
1461 return;
1463 out:
1464 complete_pdu(s, vs->pdu, err);
1465 qemu_free(vs);
1468 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1470 complete_pdu(s, vs->pdu, err);
1472 if (vs->nwnames) {
1473 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1474 v9fs_string_free(&vs->wnames[vs->name_idx]);
1477 qemu_free(vs->wnames);
1478 qemu_free(vs->qids);
1482 static void v9fs_walk_marshal(V9fsWalkState *vs)
1484 int i;
1485 vs->offset = 7;
1486 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1488 for (i = 0; i < vs->nwnames; i++) {
1489 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1493 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1494 int err)
1496 if (err == -1) {
1497 free_fid(s, vs->newfidp->fid);
1498 v9fs_string_free(&vs->path);
1499 err = -ENOENT;
1500 goto out;
1503 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1505 vs->name_idx++;
1506 if (vs->name_idx < vs->nwnames) {
1507 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1508 vs->wnames[vs->name_idx].data);
1509 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1511 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1512 v9fs_walk_post_newfid_lstat(s, vs, err);
1513 return;
1516 v9fs_string_free(&vs->path);
1517 v9fs_walk_marshal(vs);
1518 err = vs->offset;
1519 out:
1520 v9fs_walk_complete(s, vs, err);
1523 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1524 int err)
1526 if (err == -1) {
1527 v9fs_string_free(&vs->path);
1528 err = -ENOENT;
1529 goto out;
1532 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1533 vs->name_idx++;
1534 if (vs->name_idx < vs->nwnames) {
1536 v9fs_string_sprintf(&vs->path, "%s/%s",
1537 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1538 v9fs_string_copy(&vs->fidp->path, &vs->path);
1540 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1541 v9fs_walk_post_oldfid_lstat(s, vs, err);
1542 return;
1545 v9fs_string_free(&vs->path);
1546 v9fs_walk_marshal(vs);
1547 err = vs->offset;
1548 out:
1549 v9fs_walk_complete(s, vs, err);
1552 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1554 int32_t fid, newfid;
1555 V9fsWalkState *vs;
1556 int err = 0;
1557 int i;
1559 vs = qemu_malloc(sizeof(*vs));
1560 vs->pdu = pdu;
1561 vs->wnames = NULL;
1562 vs->qids = NULL;
1563 vs->offset = 7;
1565 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1566 &newfid, &vs->nwnames);
1568 if (vs->nwnames) {
1569 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1571 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1573 for (i = 0; i < vs->nwnames; i++) {
1574 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1575 &vs->wnames[i]);
1579 vs->fidp = lookup_fid(s, fid);
1580 if (vs->fidp == NULL) {
1581 err = -ENOENT;
1582 goto out;
1585 /* FIXME: is this really valid? */
1586 if (fid == newfid) {
1588 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1589 v9fs_string_init(&vs->path);
1590 vs->name_idx = 0;
1592 if (vs->name_idx < vs->nwnames) {
1593 v9fs_string_sprintf(&vs->path, "%s/%s",
1594 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1595 v9fs_string_copy(&vs->fidp->path, &vs->path);
1597 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1598 v9fs_walk_post_oldfid_lstat(s, vs, err);
1599 return;
1601 } else {
1602 vs->newfidp = alloc_fid(s, newfid);
1603 if (vs->newfidp == NULL) {
1604 err = -EINVAL;
1605 goto out;
1608 vs->newfidp->uid = vs->fidp->uid;
1609 v9fs_string_init(&vs->path);
1610 vs->name_idx = 0;
1611 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1613 if (vs->name_idx < vs->nwnames) {
1614 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1615 vs->wnames[vs->name_idx].data);
1616 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1618 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1619 v9fs_walk_post_newfid_lstat(s, vs, err);
1620 return;
1624 v9fs_walk_marshal(vs);
1625 err = vs->offset;
1626 out:
1627 v9fs_walk_complete(s, vs, err);
1630 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1632 struct statfs stbuf;
1633 int32_t iounit = 0;
1636 * iounit should be multiples of f_bsize (host filesystem block size
1637 * and as well as less than (client msize - P9_IOHDRSZ))
1639 if (!v9fs_do_statfs(s, name, &stbuf)) {
1640 iounit = stbuf.f_bsize;
1641 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1644 if (!iounit) {
1645 iounit = s->msize - P9_IOHDRSZ;
1647 return iounit;
1650 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1652 if (vs->fidp->fs.dir == NULL) {
1653 err = -errno;
1654 goto out;
1656 vs->fidp->fid_type = P9_FID_DIR;
1657 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1658 err = vs->offset;
1659 out:
1660 complete_pdu(s, vs->pdu, err);
1661 qemu_free(vs);
1665 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1667 int err;
1668 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1669 err = vs->offset;
1670 complete_pdu(s, vs->pdu, err);
1671 qemu_free(vs);
1674 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1676 if (vs->fidp->fs.fd == -1) {
1677 err = -errno;
1678 goto out;
1680 vs->fidp->fid_type = P9_FID_FILE;
1681 vs->iounit = get_iounit(s, &vs->fidp->path);
1682 v9fs_open_post_getiounit(s, vs);
1683 return;
1684 out:
1685 complete_pdu(s, vs->pdu, err);
1686 qemu_free(vs);
1689 static inline int valid_flags(int flag)
1691 if (flag & O_NOCTTY || flag & O_NONBLOCK || flag & O_ASYNC ||
1692 flag & O_CLOEXEC)
1693 return 0;
1694 else
1695 return 1;
1698 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1700 int flags;
1702 if (err) {
1703 err = -errno;
1704 goto out;
1707 stat_to_qid(&vs->stbuf, &vs->qid);
1709 if (S_ISDIR(vs->stbuf.st_mode)) {
1710 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1711 v9fs_open_post_opendir(s, vs, err);
1712 } else {
1713 if (s->proto_version == V9FS_PROTO_2000L) {
1714 if (!valid_flags(vs->mode)) {
1715 err = -EINVAL;
1716 goto out;
1718 flags = vs->mode;
1719 } else {
1720 flags = omode_to_uflags(vs->mode);
1722 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1723 v9fs_open_post_open(s, vs, err);
1725 return;
1726 out:
1727 complete_pdu(s, vs->pdu, err);
1728 qemu_free(vs);
1731 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1733 int32_t fid;
1734 V9fsOpenState *vs;
1735 ssize_t err = 0;
1737 vs = qemu_malloc(sizeof(*vs));
1738 vs->pdu = pdu;
1739 vs->offset = 7;
1740 vs->mode = 0;
1742 if (s->proto_version == V9FS_PROTO_2000L) {
1743 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1744 } else {
1745 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1748 vs->fidp = lookup_fid(s, fid);
1749 if (vs->fidp == NULL) {
1750 err = -ENOENT;
1751 goto out;
1754 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1756 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1758 v9fs_open_post_lstat(s, vs, err);
1759 return;
1760 out:
1761 complete_pdu(s, pdu, err);
1762 qemu_free(vs);
1765 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1767 if (err == 0) {
1768 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1769 stat_to_qid(&vs->stbuf, &vs->qid);
1770 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1771 &vs->iounit);
1772 err = vs->offset;
1773 } else {
1774 vs->fidp->fid_type = P9_FID_NONE;
1775 close(vs->fidp->fs.fd);
1776 err = -errno;
1779 complete_pdu(s, vs->pdu, err);
1780 v9fs_string_free(&vs->name);
1781 v9fs_string_free(&vs->fullname);
1782 qemu_free(vs);
1785 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1786 int err)
1788 if (err) {
1789 err = -errno;
1790 goto out;
1792 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1794 out:
1795 v9fs_post_lcreate(s, vs, err);
1798 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1799 int err)
1801 if (vs->fidp->fs.fd == -1) {
1802 err = -errno;
1803 goto out;
1805 vs->fidp->fid_type = P9_FID_FILE;
1806 vs->iounit = get_iounit(s, &vs->fullname);
1807 v9fs_lcreate_post_get_iounit(s, vs, err);
1808 return;
1810 out:
1811 v9fs_post_lcreate(s, vs, err);
1814 static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1816 int32_t dfid, flags, mode;
1817 gid_t gid;
1818 V9fsLcreateState *vs;
1819 ssize_t err = 0;
1821 vs = qemu_malloc(sizeof(*vs));
1822 vs->pdu = pdu;
1823 vs->offset = 7;
1825 v9fs_string_init(&vs->fullname);
1827 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1828 &mode, &gid);
1830 vs->fidp = lookup_fid(s, dfid);
1831 if (vs->fidp == NULL) {
1832 err = -ENOENT;
1833 goto out;
1836 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1837 vs->name.data);
1839 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1840 gid, flags, mode);
1841 v9fs_lcreate_post_do_open2(s, vs, err);
1842 return;
1844 out:
1845 complete_pdu(s, vs->pdu, err);
1846 v9fs_string_free(&vs->name);
1847 qemu_free(vs);
1850 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1852 int32_t fid;
1853 size_t offset = 7;
1854 int err;
1856 pdu_unmarshal(pdu, offset, "d", &fid);
1858 err = free_fid(s, fid);
1859 if (err < 0) {
1860 goto out;
1863 offset = 7;
1864 err = offset;
1865 out:
1866 complete_pdu(s, pdu, err);
1869 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1871 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1873 if (err) {
1874 goto out;
1876 v9fs_stat_free(&vs->v9stat);
1877 v9fs_string_free(&vs->name);
1878 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1879 vs->offset += vs->count;
1880 err = vs->offset;
1881 out:
1882 complete_pdu(s, vs->pdu, err);
1883 qemu_free(vs);
1884 return;
1887 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1888 ssize_t err)
1890 if (err) {
1891 err = -errno;
1892 goto out;
1894 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1895 if (err) {
1896 goto out;
1899 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1900 &vs->v9stat);
1901 if ((vs->len != (vs->v9stat.size + 2)) ||
1902 ((vs->count + vs->len) > vs->max_count)) {
1903 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1904 v9fs_read_post_seekdir(s, vs, err);
1905 return;
1907 vs->count += vs->len;
1908 v9fs_stat_free(&vs->v9stat);
1909 v9fs_string_free(&vs->name);
1910 vs->dir_pos = vs->dent->d_off;
1911 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1912 v9fs_read_post_readdir(s, vs, err);
1913 return;
1914 out:
1915 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1916 v9fs_read_post_seekdir(s, vs, err);
1917 return;
1921 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1923 if (vs->dent) {
1924 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1925 v9fs_string_init(&vs->name);
1926 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1927 vs->dent->d_name);
1928 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1929 v9fs_read_post_dir_lstat(s, vs, err);
1930 return;
1933 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1934 vs->offset += vs->count;
1935 err = vs->offset;
1936 complete_pdu(s, vs->pdu, err);
1937 qemu_free(vs);
1938 return;
1941 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1943 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1944 v9fs_read_post_readdir(s, vs, err);
1945 return;
1948 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1949 ssize_t err)
1951 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1952 v9fs_read_post_telldir(s, vs, err);
1953 return;
1956 static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1958 if (err < 0) {
1959 /* IO error return the error */
1960 err = -errno;
1961 goto out;
1963 vs->total += vs->len;
1964 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1965 if (vs->total < vs->count && vs->len > 0) {
1966 do {
1967 if (0) {
1968 print_sg(vs->sg, vs->cnt);
1970 vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1971 } while (vs->len == -1 && errno == EINTR);
1972 if (vs->len == -1) {
1973 err = -errno;
1975 v9fs_read_post_readv(s, vs, err);
1976 return;
1978 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1979 vs->offset += vs->count;
1980 err = vs->offset;
1982 out:
1983 complete_pdu(s, vs->pdu, err);
1984 qemu_free(vs);
1987 static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1989 if (err == -1) {
1990 err = -errno;
1991 goto out;
1993 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1995 if (vs->total < vs->count) {
1996 do {
1997 if (0) {
1998 print_sg(vs->sg, vs->cnt);
2000 vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2001 } while (vs->len == -1 && errno == EINTR);
2002 if (vs->len == -1) {
2003 err = -errno;
2005 v9fs_read_post_readv(s, vs, err);
2006 return;
2008 out:
2009 complete_pdu(s, vs->pdu, err);
2010 qemu_free(vs);
2013 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2015 ssize_t err = 0;
2016 int read_count;
2017 int64_t xattr_len;
2019 xattr_len = vs->fidp->fs.xattr.len;
2020 read_count = xattr_len - vs->off;
2021 if (read_count > vs->count) {
2022 read_count = vs->count;
2023 } else if (read_count < 0) {
2025 * read beyond XATTR value
2027 read_count = 0;
2029 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2030 vs->offset += pdu_pack(vs->pdu, vs->offset,
2031 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2032 read_count);
2033 err = vs->offset;
2034 complete_pdu(s, vs->pdu, err);
2035 qemu_free(vs);
2038 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2040 int32_t fid;
2041 V9fsReadState *vs;
2042 ssize_t err = 0;
2044 vs = qemu_malloc(sizeof(*vs));
2045 vs->pdu = pdu;
2046 vs->offset = 7;
2047 vs->total = 0;
2048 vs->len = 0;
2049 vs->count = 0;
2051 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2053 vs->fidp = lookup_fid(s, fid);
2054 if (vs->fidp == NULL) {
2055 err = -EINVAL;
2056 goto out;
2059 if (vs->fidp->fid_type == P9_FID_DIR) {
2060 vs->max_count = vs->count;
2061 vs->count = 0;
2062 if (vs->off == 0) {
2063 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2065 v9fs_read_post_rewinddir(s, vs, err);
2066 return;
2067 } else if (vs->fidp->fid_type == P9_FID_FILE) {
2068 vs->sg = vs->iov;
2069 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2070 err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2071 v9fs_read_post_lseek(s, vs, err);
2072 return;
2073 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2074 v9fs_xattr_read(s, vs);
2075 return;
2076 } else {
2077 err = -EINVAL;
2079 out:
2080 complete_pdu(s, pdu, err);
2081 qemu_free(vs);
2084 typedef struct V9fsReadDirState {
2085 V9fsPDU *pdu;
2086 V9fsFidState *fidp;
2087 V9fsQID qid;
2088 off_t saved_dir_pos;
2089 struct dirent *dent;
2090 int32_t count;
2091 int32_t max_count;
2092 size_t offset;
2093 int64_t initial_offset;
2094 V9fsString name;
2095 } V9fsReadDirState;
2097 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2099 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2100 vs->offset += vs->count;
2101 complete_pdu(s, vs->pdu, vs->offset);
2102 qemu_free(vs);
2103 return;
2106 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2107 * size of type (1) + size of name.size (2) + strlen(name.data)
2109 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2111 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2113 int len;
2114 size_t size;
2116 if (vs->dent) {
2117 v9fs_string_init(&vs->name);
2118 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2120 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2121 /* Ran out of buffer. Set dir back to old position and return */
2122 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2123 v9fs_readdir_post_seekdir(s, vs);
2124 return;
2127 /* Fill up just the path field of qid because the client uses
2128 * only that. To fill the entire qid structure we will have
2129 * to stat each dirent found, which is expensive
2131 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2132 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2133 /* Fill the other fields with dummy values */
2134 vs->qid.type = 0;
2135 vs->qid.version = 0;
2137 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2138 &vs->qid, vs->dent->d_off,
2139 vs->dent->d_type, &vs->name);
2140 vs->count += len;
2141 v9fs_string_free(&vs->name);
2142 vs->saved_dir_pos = vs->dent->d_off;
2143 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2144 v9fs_readdir_post_readdir(s, vs);
2145 return;
2148 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2149 vs->offset += vs->count;
2150 complete_pdu(s, vs->pdu, vs->offset);
2151 qemu_free(vs);
2152 return;
2155 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2157 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2158 v9fs_readdir_post_readdir(s, vs);
2159 return;
2162 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2164 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2165 v9fs_readdir_post_telldir(s, vs);
2166 return;
2169 static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2171 int32_t fid;
2172 V9fsReadDirState *vs;
2173 ssize_t err = 0;
2174 size_t offset = 7;
2176 vs = qemu_malloc(sizeof(*vs));
2177 vs->pdu = pdu;
2178 vs->offset = 7;
2179 vs->count = 0;
2181 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2182 &vs->max_count);
2184 vs->fidp = lookup_fid(s, fid);
2185 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2186 err = -EINVAL;
2187 goto out;
2190 if (vs->initial_offset == 0) {
2191 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2192 } else {
2193 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2196 v9fs_readdir_post_setdir(s, vs);
2197 return;
2199 out:
2200 complete_pdu(s, pdu, err);
2201 qemu_free(vs);
2202 return;
2205 static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
2206 ssize_t err)
2208 if (err < 0) {
2209 /* IO error return the error */
2210 err = -errno;
2211 goto out;
2213 vs->total += vs->len;
2214 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2215 if (vs->total < vs->count && vs->len > 0) {
2216 do {
2217 if (0) {
2218 print_sg(vs->sg, vs->cnt);
2220 vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2221 } while (vs->len == -1 && errno == EINTR);
2222 if (vs->len == -1) {
2223 err = -errno;
2225 v9fs_write_post_writev(s, vs, err);
2226 return;
2228 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2230 err = vs->offset;
2231 out:
2232 complete_pdu(s, vs->pdu, err);
2233 qemu_free(vs);
2236 static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
2238 if (err == -1) {
2239 err = -errno;
2240 goto out;
2242 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2244 if (vs->total < vs->count) {
2245 do {
2246 if (0) {
2247 print_sg(vs->sg, vs->cnt);
2249 vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2250 } while (vs->len == -1 && errno == EINTR);
2251 if (vs->len == -1) {
2252 err = -errno;
2254 v9fs_write_post_writev(s, vs, err);
2255 return;
2258 out:
2259 complete_pdu(s, vs->pdu, err);
2260 qemu_free(vs);
2263 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2265 int i, to_copy;
2266 ssize_t err = 0;
2267 int write_count;
2268 int64_t xattr_len;
2270 xattr_len = vs->fidp->fs.xattr.len;
2271 write_count = xattr_len - vs->off;
2272 if (write_count > vs->count) {
2273 write_count = vs->count;
2274 } else if (write_count < 0) {
2276 * write beyond XATTR value len specified in
2277 * xattrcreate
2279 err = -ENOSPC;
2280 goto out;
2282 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2283 err = vs->offset;
2284 vs->fidp->fs.xattr.copied_len += write_count;
2286 * Now copy the content from sg list
2288 for (i = 0; i < vs->cnt; i++) {
2289 if (write_count > vs->sg[i].iov_len) {
2290 to_copy = vs->sg[i].iov_len;
2291 } else {
2292 to_copy = write_count;
2294 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2295 vs->sg[i].iov_base, to_copy);
2296 /* updating vs->off since we are not using below */
2297 vs->off += to_copy;
2298 write_count -= to_copy;
2300 out:
2301 complete_pdu(s, vs->pdu, err);
2302 qemu_free(vs);
2305 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2307 int32_t fid;
2308 V9fsWriteState *vs;
2309 ssize_t err;
2311 vs = qemu_malloc(sizeof(*vs));
2313 vs->pdu = pdu;
2314 vs->offset = 7;
2315 vs->sg = vs->iov;
2316 vs->total = 0;
2317 vs->len = 0;
2319 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2320 vs->sg, &vs->cnt);
2322 vs->fidp = lookup_fid(s, fid);
2323 if (vs->fidp == NULL) {
2324 err = -EINVAL;
2325 goto out;
2328 if (vs->fidp->fid_type == P9_FID_FILE) {
2329 if (vs->fidp->fs.fd == -1) {
2330 err = -EINVAL;
2331 goto out;
2333 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2335 * setxattr operation
2337 v9fs_xattr_write(s, vs);
2338 return;
2339 } else {
2340 err = -EINVAL;
2341 goto out;
2343 err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2345 v9fs_write_post_lseek(s, vs, err);
2346 return;
2348 out:
2349 complete_pdu(s, vs->pdu, err);
2350 qemu_free(vs);
2353 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2355 int err;
2356 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2357 stat_to_qid(&vs->stbuf, &vs->qid);
2359 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2360 err = vs->offset;
2362 complete_pdu(s, vs->pdu, err);
2363 v9fs_string_free(&vs->name);
2364 v9fs_string_free(&vs->extension);
2365 v9fs_string_free(&vs->fullname);
2366 qemu_free(vs);
2369 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2371 if (err == 0) {
2372 vs->iounit = get_iounit(s, &vs->fidp->path);
2373 v9fs_create_post_getiounit(s, vs);
2374 return;
2377 complete_pdu(s, vs->pdu, err);
2378 v9fs_string_free(&vs->name);
2379 v9fs_string_free(&vs->extension);
2380 v9fs_string_free(&vs->fullname);
2381 qemu_free(vs);
2384 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2386 if (err) {
2387 err = -errno;
2389 v9fs_post_create(s, vs, err);
2392 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2393 int err)
2395 if (!vs->fidp->fs.dir) {
2396 err = -errno;
2398 vs->fidp->fid_type = P9_FID_DIR;
2399 v9fs_post_create(s, vs, err);
2402 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2403 int err)
2405 if (err) {
2406 err = -errno;
2407 goto out;
2410 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2411 v9fs_create_post_opendir(s, vs, err);
2412 return;
2414 out:
2415 v9fs_post_create(s, vs, err);
2418 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2420 if (err) {
2421 err = -errno;
2422 goto out;
2425 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2426 v9fs_create_post_dir_lstat(s, vs, err);
2427 return;
2429 out:
2430 v9fs_post_create(s, vs, err);
2433 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2435 if (err) {
2436 vs->fidp->fid_type = P9_FID_NONE;
2437 close(vs->fidp->fs.fd);
2438 err = -errno;
2440 v9fs_post_create(s, vs, err);
2441 return;
2444 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2446 if (vs->fidp->fs.fd == -1) {
2447 err = -errno;
2448 goto out;
2450 vs->fidp->fid_type = P9_FID_FILE;
2451 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2452 v9fs_create_post_fstat(s, vs, err);
2454 return;
2456 out:
2457 v9fs_post_create(s, vs, err);
2461 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2464 if (err == 0 || errno != ENOENT) {
2465 err = -errno;
2466 goto out;
2469 if (vs->perm & P9_STAT_MODE_DIR) {
2470 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2471 vs->fidp->uid, -1);
2472 v9fs_create_post_mkdir(s, vs, err);
2473 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2474 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2475 vs->fullname.data, -1);
2476 v9fs_create_post_perms(s, vs, err);
2477 } else if (vs->perm & P9_STAT_MODE_LINK) {
2478 int32_t nfid = atoi(vs->extension.data);
2479 V9fsFidState *nfidp = lookup_fid(s, nfid);
2480 if (nfidp == NULL) {
2481 err = -errno;
2482 v9fs_post_create(s, vs, err);
2484 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2485 v9fs_create_post_perms(s, vs, err);
2486 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2487 char ctype;
2488 uint32_t major, minor;
2489 mode_t nmode = 0;
2491 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2492 &minor) != 3) {
2493 err = -errno;
2494 v9fs_post_create(s, vs, err);
2497 switch (ctype) {
2498 case 'c':
2499 nmode = S_IFCHR;
2500 break;
2501 case 'b':
2502 nmode = S_IFBLK;
2503 break;
2504 default:
2505 err = -EIO;
2506 v9fs_post_create(s, vs, err);
2509 nmode |= vs->perm & 0777;
2510 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2511 makedev(major, minor), vs->fidp->uid, -1);
2512 v9fs_create_post_perms(s, vs, err);
2513 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2514 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2515 0, vs->fidp->uid, -1);
2516 v9fs_post_create(s, vs, err);
2517 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2518 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2519 0, vs->fidp->uid, -1);
2520 v9fs_post_create(s, vs, err);
2521 } else {
2522 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2523 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2525 v9fs_create_post_open2(s, vs, err);
2528 return;
2530 out:
2531 v9fs_post_create(s, vs, err);
2534 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2536 int32_t fid;
2537 V9fsCreateState *vs;
2538 int err = 0;
2540 vs = qemu_malloc(sizeof(*vs));
2541 vs->pdu = pdu;
2542 vs->offset = 7;
2544 v9fs_string_init(&vs->fullname);
2546 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2547 &vs->perm, &vs->mode, &vs->extension);
2549 vs->fidp = lookup_fid(s, fid);
2550 if (vs->fidp == NULL) {
2551 err = -EINVAL;
2552 goto out;
2555 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2556 vs->name.data);
2558 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2559 v9fs_create_post_lstat(s, vs, err);
2560 return;
2562 out:
2563 complete_pdu(s, vs->pdu, err);
2564 v9fs_string_free(&vs->name);
2565 v9fs_string_free(&vs->extension);
2566 qemu_free(vs);
2569 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2571 if (err == 0) {
2572 stat_to_qid(&vs->stbuf, &vs->qid);
2573 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2574 err = vs->offset;
2575 } else {
2576 err = -errno;
2578 complete_pdu(s, vs->pdu, err);
2579 v9fs_string_free(&vs->name);
2580 v9fs_string_free(&vs->symname);
2581 v9fs_string_free(&vs->fullname);
2582 qemu_free(vs);
2585 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2586 int err)
2588 if (err) {
2589 goto out;
2591 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2592 out:
2593 v9fs_post_symlink(s, vs, err);
2596 static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2598 int32_t dfid;
2599 V9fsSymlinkState *vs;
2600 int err = 0;
2601 gid_t gid;
2603 vs = qemu_malloc(sizeof(*vs));
2604 vs->pdu = pdu;
2605 vs->offset = 7;
2607 v9fs_string_init(&vs->fullname);
2609 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2610 &vs->symname, &gid);
2612 vs->dfidp = lookup_fid(s, dfid);
2613 if (vs->dfidp == NULL) {
2614 err = -EINVAL;
2615 goto out;
2618 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2619 vs->name.data);
2620 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2621 vs->fullname.data, gid);
2622 v9fs_symlink_post_do_symlink(s, vs, err);
2623 return;
2625 out:
2626 complete_pdu(s, vs->pdu, err);
2627 v9fs_string_free(&vs->name);
2628 v9fs_string_free(&vs->symname);
2629 qemu_free(vs);
2632 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2634 /* A nop call with no return */
2635 complete_pdu(s, pdu, 7);
2638 static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2640 int32_t dfid, oldfid;
2641 V9fsFidState *dfidp, *oldfidp;
2642 V9fsString name, fullname;
2643 size_t offset = 7;
2644 int err = 0;
2646 v9fs_string_init(&fullname);
2648 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2650 dfidp = lookup_fid(s, dfid);
2651 if (dfidp == NULL) {
2652 err = -errno;
2653 goto out;
2656 oldfidp = lookup_fid(s, oldfid);
2657 if (oldfidp == NULL) {
2658 err = -errno;
2659 goto out;
2662 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2663 err = offset;
2664 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2665 if (err) {
2666 err = -errno;
2668 v9fs_string_free(&fullname);
2670 out:
2671 v9fs_string_free(&name);
2672 complete_pdu(s, pdu, err);
2675 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2676 int err)
2678 if (err < 0) {
2679 err = -errno;
2680 } else {
2681 err = vs->offset;
2684 /* For TREMOVE we need to clunk the fid even on failed remove */
2685 free_fid(s, vs->fidp->fid);
2687 complete_pdu(s, vs->pdu, err);
2688 qemu_free(vs);
2691 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2693 int32_t fid;
2694 V9fsRemoveState *vs;
2695 int err = 0;
2697 vs = qemu_malloc(sizeof(*vs));
2698 vs->pdu = pdu;
2699 vs->offset = 7;
2701 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2703 vs->fidp = lookup_fid(s, fid);
2704 if (vs->fidp == NULL) {
2705 err = -EINVAL;
2706 goto out;
2709 err = v9fs_do_remove(s, &vs->fidp->path);
2710 v9fs_remove_post_remove(s, vs, err);
2711 return;
2713 out:
2714 complete_pdu(s, pdu, err);
2715 qemu_free(vs);
2718 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2720 if (err < 0) {
2721 goto out;
2724 err = vs->offset;
2726 out:
2727 v9fs_stat_free(&vs->v9stat);
2728 complete_pdu(s, vs->pdu, err);
2729 qemu_free(vs);
2732 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2734 if (err < 0) {
2735 goto out;
2737 if (vs->v9stat.length != -1) {
2738 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2739 err = -errno;
2742 v9fs_wstat_post_truncate(s, vs, err);
2743 return;
2745 out:
2746 v9fs_stat_free(&vs->v9stat);
2747 complete_pdu(s, vs->pdu, err);
2748 qemu_free(vs);
2751 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2753 int err = 0;
2754 char *old_name, *new_name;
2755 char *end;
2757 if (vs->newdirfid != -1) {
2758 V9fsFidState *dirfidp;
2759 dirfidp = lookup_fid(s, vs->newdirfid);
2761 if (dirfidp == NULL) {
2762 err = -ENOENT;
2763 goto out;
2766 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2768 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2770 strcpy(new_name, dirfidp->path.data);
2771 strcat(new_name, "/");
2772 strcat(new_name + dirfidp->path.size, vs->name.data);
2773 } else {
2774 old_name = vs->fidp->path.data;
2775 end = strrchr(old_name, '/');
2776 if (end) {
2777 end++;
2778 } else {
2779 end = old_name;
2781 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2783 strncat(new_name, old_name, end - old_name);
2784 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2787 v9fs_string_free(&vs->name);
2788 vs->name.data = qemu_strdup(new_name);
2789 vs->name.size = strlen(new_name);
2791 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2792 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2793 err = -errno;
2794 } else {
2795 V9fsFidState *fidp;
2797 * Fixup fid's pointing to the old name to
2798 * start pointing to the new name
2800 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2801 if (vs->fidp == fidp) {
2803 * we replace name of this fid towards the end
2804 * so that our below strcmp will work
2806 continue;
2808 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2809 strlen(vs->fidp->path.data))) {
2810 /* replace the name */
2811 v9fs_fix_path(&fidp->path, &vs->name,
2812 strlen(vs->fidp->path.data));
2815 v9fs_string_copy(&vs->fidp->path, &vs->name);
2818 out:
2819 v9fs_string_free(&vs->name);
2820 return err;
2823 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2825 complete_pdu(s, vs->pdu, err);
2826 qemu_free(vs);
2829 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2831 if (err < 0) {
2832 goto out;
2835 if (vs->v9stat.name.size != 0) {
2836 V9fsRenameState *vr;
2838 vr = qemu_mallocz(sizeof(V9fsRenameState));
2839 vr->newdirfid = -1;
2840 vr->pdu = vs->pdu;
2841 vr->fidp = vs->fidp;
2842 vr->offset = vs->offset;
2843 vr->name.size = vs->v9stat.name.size;
2844 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2846 err = v9fs_complete_rename(s, vr);
2847 qemu_free(vr);
2849 v9fs_wstat_post_rename(s, vs, err);
2850 return;
2852 out:
2853 v9fs_stat_free(&vs->v9stat);
2854 complete_pdu(s, vs->pdu, err);
2855 qemu_free(vs);
2858 static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2860 int32_t fid;
2861 V9fsRenameState *vs;
2862 ssize_t err = 0;
2864 vs = qemu_malloc(sizeof(*vs));
2865 vs->pdu = pdu;
2866 vs->offset = 7;
2868 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2870 vs->fidp = lookup_fid(s, fid);
2871 if (vs->fidp == NULL) {
2872 err = -ENOENT;
2873 goto out;
2876 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2878 err = v9fs_complete_rename(s, vs);
2879 v9fs_rename_post_rename(s, vs, err);
2880 return;
2881 out:
2882 complete_pdu(s, vs->pdu, err);
2883 qemu_free(vs);
2886 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2888 if (err < 0) {
2889 goto out;
2892 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2893 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2894 vs->v9stat.n_gid)) {
2895 err = -errno;
2898 v9fs_wstat_post_chown(s, vs, err);
2899 return;
2901 out:
2902 v9fs_stat_free(&vs->v9stat);
2903 complete_pdu(s, vs->pdu, err);
2904 qemu_free(vs);
2907 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2909 if (err < 0) {
2910 goto out;
2913 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2914 struct timespec times[2];
2915 if (vs->v9stat.atime != -1) {
2916 times[0].tv_sec = vs->v9stat.atime;
2917 times[0].tv_nsec = 0;
2918 } else {
2919 times[0].tv_nsec = UTIME_OMIT;
2921 if (vs->v9stat.mtime != -1) {
2922 times[1].tv_sec = vs->v9stat.mtime;
2923 times[1].tv_nsec = 0;
2924 } else {
2925 times[1].tv_nsec = UTIME_OMIT;
2928 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2929 err = -errno;
2933 v9fs_wstat_post_utime(s, vs, err);
2934 return;
2936 out:
2937 v9fs_stat_free(&vs->v9stat);
2938 complete_pdu(s, vs->pdu, err);
2939 qemu_free(vs);
2942 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2944 if (err == -1) {
2945 err = -errno;
2947 v9fs_stat_free(&vs->v9stat);
2948 complete_pdu(s, vs->pdu, err);
2949 qemu_free(vs);
2952 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2954 uint32_t v9_mode;
2956 if (err == -1) {
2957 err = -errno;
2958 goto out;
2961 v9_mode = stat_to_v9mode(&vs->stbuf);
2963 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2964 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2965 /* Attempting to change the type */
2966 err = -EIO;
2967 goto out;
2970 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2971 &vs->v9stat.extension))) {
2972 err = -errno;
2974 v9fs_wstat_post_chmod(s, vs, err);
2975 return;
2977 out:
2978 v9fs_stat_free(&vs->v9stat);
2979 complete_pdu(s, vs->pdu, err);
2980 qemu_free(vs);
2983 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2985 int32_t fid;
2986 V9fsWstatState *vs;
2987 int err = 0;
2989 vs = qemu_malloc(sizeof(*vs));
2990 vs->pdu = pdu;
2991 vs->offset = 7;
2993 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2995 vs->fidp = lookup_fid(s, fid);
2996 if (vs->fidp == NULL) {
2997 err = -EINVAL;
2998 goto out;
3001 /* do we need to sync the file? */
3002 if (donttouch_stat(&vs->v9stat)) {
3003 err = v9fs_do_fsync(s, vs->fidp->fs.fd);
3004 v9fs_wstat_post_fsync(s, vs, err);
3005 return;
3008 if (vs->v9stat.mode != -1) {
3009 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3010 v9fs_wstat_post_lstat(s, vs, err);
3011 return;
3014 v9fs_wstat_post_chmod(s, vs, err);
3015 return;
3017 out:
3018 v9fs_stat_free(&vs->v9stat);
3019 complete_pdu(s, vs->pdu, err);
3020 qemu_free(vs);
3023 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3025 int32_t bsize_factor;
3027 if (err) {
3028 err = -errno;
3029 goto out;
3033 * compute bsize factor based on host file system block size
3034 * and client msize
3036 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3037 if (!bsize_factor) {
3038 bsize_factor = 1;
3040 vs->v9statfs.f_type = vs->stbuf.f_type;
3041 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3042 vs->v9statfs.f_bsize *= bsize_factor;
3044 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3045 * adjust(divide) the number of blocks, free blocks and available
3046 * blocks by bsize factor
3048 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3049 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3050 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3051 vs->v9statfs.f_files = vs->stbuf.f_files;
3052 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3053 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3054 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3055 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3057 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3058 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3059 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3060 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3061 vs->v9statfs.f_namelen);
3063 out:
3064 complete_pdu(s, vs->pdu, vs->offset);
3065 qemu_free(vs);
3068 static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3070 V9fsStatfsState *vs;
3071 ssize_t err = 0;
3073 vs = qemu_malloc(sizeof(*vs));
3074 vs->pdu = pdu;
3075 vs->offset = 7;
3077 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3079 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3081 vs->fidp = lookup_fid(s, vs->fid);
3082 if (vs->fidp == NULL) {
3083 err = -ENOENT;
3084 goto out;
3087 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3088 v9fs_statfs_post_statfs(s, vs, err);
3089 return;
3091 out:
3092 complete_pdu(s, vs->pdu, err);
3093 qemu_free(vs);
3096 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3098 if (err == -1) {
3099 err = -errno;
3100 goto out;
3103 stat_to_qid(&vs->stbuf, &vs->qid);
3104 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3105 err = vs->offset;
3106 out:
3107 complete_pdu(s, vs->pdu, err);
3108 v9fs_string_free(&vs->fullname);
3109 v9fs_string_free(&vs->name);
3110 qemu_free(vs);
3113 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3115 if (err == -1) {
3116 err = -errno;
3117 goto out;
3120 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3121 v9fs_mknod_post_lstat(s, vs, err);
3122 return;
3123 out:
3124 complete_pdu(s, vs->pdu, err);
3125 v9fs_string_free(&vs->fullname);
3126 v9fs_string_free(&vs->name);
3127 qemu_free(vs);
3130 static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3132 int32_t fid;
3133 V9fsMkState *vs;
3134 int err = 0;
3135 V9fsFidState *fidp;
3136 gid_t gid;
3137 int mode;
3138 int major, minor;
3140 vs = qemu_malloc(sizeof(*vs));
3141 vs->pdu = pdu;
3142 vs->offset = 7;
3144 v9fs_string_init(&vs->fullname);
3145 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3146 &major, &minor, &gid);
3148 fidp = lookup_fid(s, fid);
3149 if (fidp == NULL) {
3150 err = -ENOENT;
3151 goto out;
3154 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3155 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3156 fidp->uid, gid);
3157 v9fs_mknod_post_mknod(s, vs, err);
3158 return;
3160 out:
3161 complete_pdu(s, vs->pdu, err);
3162 v9fs_string_free(&vs->fullname);
3163 v9fs_string_free(&vs->name);
3164 qemu_free(vs);
3167 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3169 if (err == -1) {
3170 err = -errno;
3171 goto out;
3174 stat_to_qid(&vs->stbuf, &vs->qid);
3175 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3176 err = vs->offset;
3177 out:
3178 complete_pdu(s, vs->pdu, err);
3179 v9fs_string_free(&vs->fullname);
3180 v9fs_string_free(&vs->name);
3181 qemu_free(vs);
3184 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3186 if (err == -1) {
3187 err = -errno;
3188 goto out;
3191 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3192 v9fs_mkdir_post_lstat(s, vs, err);
3193 return;
3194 out:
3195 complete_pdu(s, vs->pdu, err);
3196 v9fs_string_free(&vs->fullname);
3197 v9fs_string_free(&vs->name);
3198 qemu_free(vs);
3201 static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3203 int32_t fid;
3204 V9fsMkState *vs;
3205 int err = 0;
3206 V9fsFidState *fidp;
3207 gid_t gid;
3208 int mode;
3210 vs = qemu_malloc(sizeof(*vs));
3211 vs->pdu = pdu;
3212 vs->offset = 7;
3214 v9fs_string_init(&vs->fullname);
3215 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3216 &gid);
3218 fidp = lookup_fid(s, fid);
3219 if (fidp == NULL) {
3220 err = -ENOENT;
3221 goto out;
3224 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3225 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3226 v9fs_mkdir_post_mkdir(s, vs, err);
3227 return;
3229 out:
3230 complete_pdu(s, vs->pdu, err);
3231 v9fs_string_free(&vs->fullname);
3232 v9fs_string_free(&vs->name);
3233 qemu_free(vs);
3236 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3239 if (err < 0) {
3240 err = -errno;
3241 free_fid(s, vs->xattr_fidp->fid);
3242 goto out;
3244 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3245 err = vs->offset;
3246 out:
3247 complete_pdu(s, vs->pdu, err);
3248 v9fs_string_free(&vs->name);
3249 qemu_free(vs);
3250 return;
3253 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3255 if (err < 0) {
3256 err = -errno;
3257 free_fid(s, vs->xattr_fidp->fid);
3258 goto out;
3261 * Read the xattr value
3263 vs->xattr_fidp->fs.xattr.len = vs->size;
3264 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3265 vs->xattr_fidp->fs.xattr.copied_len = -1;
3266 if (vs->size) {
3267 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3268 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3269 &vs->name, vs->xattr_fidp->fs.xattr.value,
3270 vs->xattr_fidp->fs.xattr.len);
3272 v9fs_post_xattr_getvalue(s, vs, err);
3273 return;
3274 out:
3275 complete_pdu(s, vs->pdu, err);
3276 v9fs_string_free(&vs->name);
3277 qemu_free(vs);
3280 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3281 V9fsXattrState *vs, int err)
3283 if (err < 0) {
3284 err = -errno;
3285 free_fid(s, vs->xattr_fidp->fid);
3286 goto out;
3288 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3289 err = vs->offset;
3290 out:
3291 complete_pdu(s, vs->pdu, err);
3292 v9fs_string_free(&vs->name);
3293 qemu_free(vs);
3294 return;
3297 static void v9fs_post_lxattr_check(V9fsState *s,
3298 V9fsXattrState *vs, ssize_t err)
3300 if (err < 0) {
3301 err = -errno;
3302 free_fid(s, vs->xattr_fidp->fid);
3303 goto out;
3306 * Read the xattr value
3308 vs->xattr_fidp->fs.xattr.len = vs->size;
3309 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3310 vs->xattr_fidp->fs.xattr.copied_len = -1;
3311 if (vs->size) {
3312 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3313 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3314 vs->xattr_fidp->fs.xattr.value,
3315 vs->xattr_fidp->fs.xattr.len);
3317 v9fs_post_lxattr_getvalue(s, vs, err);
3318 return;
3319 out:
3320 complete_pdu(s, vs->pdu, err);
3321 v9fs_string_free(&vs->name);
3322 qemu_free(vs);
3325 static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3327 ssize_t err = 0;
3328 V9fsXattrState *vs;
3329 int32_t fid, newfid;
3331 vs = qemu_malloc(sizeof(*vs));
3332 vs->pdu = pdu;
3333 vs->offset = 7;
3335 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3336 vs->file_fidp = lookup_fid(s, fid);
3337 if (vs->file_fidp == NULL) {
3338 err = -ENOENT;
3339 goto out;
3342 vs->xattr_fidp = alloc_fid(s, newfid);
3343 if (vs->xattr_fidp == NULL) {
3344 err = -EINVAL;
3345 goto out;
3348 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3349 if (vs->name.data[0] == 0) {
3351 * listxattr request. Get the size first
3353 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3354 NULL, 0);
3355 if (vs->size < 0) {
3356 err = vs->size;
3358 v9fs_post_lxattr_check(s, vs, err);
3359 return;
3360 } else {
3362 * specific xattr fid. We check for xattr
3363 * presence also collect the xattr size
3365 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3366 &vs->name, NULL, 0);
3367 if (vs->size < 0) {
3368 err = vs->size;
3370 v9fs_post_xattr_check(s, vs, err);
3371 return;
3373 out:
3374 complete_pdu(s, vs->pdu, err);
3375 v9fs_string_free(&vs->name);
3376 qemu_free(vs);
3379 static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3381 int flags;
3382 int32_t fid;
3383 ssize_t err = 0;
3384 V9fsXattrState *vs;
3386 vs = qemu_malloc(sizeof(*vs));
3387 vs->pdu = pdu;
3388 vs->offset = 7;
3390 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3391 &fid, &vs->name, &vs->size, &flags);
3393 vs->file_fidp = lookup_fid(s, fid);
3394 if (vs->file_fidp == NULL) {
3395 err = -EINVAL;
3396 goto out;
3399 /* Make the file fid point to xattr */
3400 vs->xattr_fidp = vs->file_fidp;
3401 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3402 vs->xattr_fidp->fs.xattr.copied_len = 0;
3403 vs->xattr_fidp->fs.xattr.len = vs->size;
3404 vs->xattr_fidp->fs.xattr.flags = flags;
3405 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3406 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3407 if (vs->size)
3408 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3409 else
3410 vs->xattr_fidp->fs.xattr.value = NULL;
3412 out:
3413 complete_pdu(s, vs->pdu, err);
3414 v9fs_string_free(&vs->name);
3415 qemu_free(vs);
3418 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3420 static pdu_handler_t *pdu_handlers[] = {
3421 [P9_TREADDIR] = v9fs_readdir,
3422 [P9_TSTATFS] = v9fs_statfs,
3423 [P9_TGETATTR] = v9fs_getattr,
3424 [P9_TSETATTR] = v9fs_setattr,
3425 [P9_TXATTRWALK] = v9fs_xattrwalk,
3426 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3427 [P9_TMKNOD] = v9fs_mknod,
3428 [P9_TRENAME] = v9fs_rename,
3429 [P9_TMKDIR] = v9fs_mkdir,
3430 [P9_TVERSION] = v9fs_version,
3431 [P9_TLOPEN] = v9fs_open,
3432 [P9_TATTACH] = v9fs_attach,
3433 [P9_TSTAT] = v9fs_stat,
3434 [P9_TWALK] = v9fs_walk,
3435 [P9_TCLUNK] = v9fs_clunk,
3436 [P9_TOPEN] = v9fs_open,
3437 [P9_TREAD] = v9fs_read,
3438 #if 0
3439 [P9_TAUTH] = v9fs_auth,
3440 #endif
3441 [P9_TFLUSH] = v9fs_flush,
3442 [P9_TLINK] = v9fs_link,
3443 [P9_TSYMLINK] = v9fs_symlink,
3444 [P9_TCREATE] = v9fs_create,
3445 [P9_TLCREATE] = v9fs_lcreate,
3446 [P9_TWRITE] = v9fs_write,
3447 [P9_TWSTAT] = v9fs_wstat,
3448 [P9_TREMOVE] = v9fs_remove,
3451 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3453 pdu_handler_t *handler;
3455 if (debug_9p_pdu) {
3456 pprint_pdu(pdu);
3459 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
3461 handler = pdu_handlers[pdu->id];
3462 BUG_ON(handler == NULL);
3464 handler(s, pdu);
3467 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3469 V9fsState *s = (V9fsState *)vdev;
3470 V9fsPDU *pdu;
3471 ssize_t len;
3473 while ((pdu = alloc_pdu(s)) &&
3474 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3475 uint8_t *ptr;
3477 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3478 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3480 ptr = pdu->elem.out_sg[0].iov_base;
3482 memcpy(&pdu->size, ptr, 4);
3483 pdu->id = ptr[4];
3484 memcpy(&pdu->tag, ptr + 5, 2);
3486 submit_pdu(s, pdu);
3489 free_pdu(s, pdu);
3492 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3494 features |= 1 << VIRTIO_9P_MOUNT_TAG;
3495 return features;
3498 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3500 return (V9fsState *)vdev;
3503 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
3505 struct virtio_9p_config *cfg;
3506 V9fsState *s = to_virtio_9p(vdev);
3508 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
3509 s->tag_len);
3510 stw_raw(&cfg->tag_len, s->tag_len);
3511 memcpy(cfg->tag, s->tag, s->tag_len);
3512 memcpy(config, cfg, s->config_size);
3513 qemu_free(cfg);
3516 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
3518 V9fsState *s;
3519 int i, len;
3520 struct stat stat;
3521 FsTypeEntry *fse;
3524 s = (V9fsState *)virtio_common_init("virtio-9p",
3525 VIRTIO_ID_9P,
3526 sizeof(struct virtio_9p_config)+
3527 MAX_TAG_LEN,
3528 sizeof(V9fsState));
3530 /* initialize pdu allocator */
3531 QLIST_INIT(&s->free_list);
3532 for (i = 0; i < (MAX_REQ - 1); i++) {
3533 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3536 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
3538 fse = get_fsdev_fsentry(conf->fsdev_id);
3540 if (!fse) {
3541 /* We don't have a fsdev identified by fsdev_id */
3542 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
3543 "with the id %s\n", conf->fsdev_id);
3544 exit(1);
3547 if (!fse->path || !conf->tag) {
3548 /* we haven't specified a mount_tag or the path */
3549 fprintf(stderr, "fsdev with id %s needs path "
3550 "and Virtio-9p device needs mount_tag arguments\n",
3551 conf->fsdev_id);
3552 exit(1);
3555 if (!strcmp(fse->security_model, "passthrough")) {
3556 /* Files on the Fileserver set to client user credentials */
3557 s->ctx.fs_sm = SM_PASSTHROUGH;
3558 } else if (!strcmp(fse->security_model, "mapped")) {
3559 /* Files on the fileserver are set to QEMU credentials.
3560 * Client user credentials are saved in extended attributes.
3562 s->ctx.fs_sm = SM_MAPPED;
3563 } else if (!strcmp(fse->security_model, "none")) {
3565 * Files on the fileserver are set to QEMU credentials.
3567 s->ctx.fs_sm = SM_NONE;
3569 } else {
3570 fprintf(stderr, "Default to security_model=none. You may want"
3571 " enable advanced security model using "
3572 "security option:\n\t security_model=passthrough \n\t "
3573 "security_model=mapped\n");
3574 s->ctx.fs_sm = SM_NONE;
3577 if (lstat(fse->path, &stat)) {
3578 fprintf(stderr, "share path %s does not exist\n", fse->path);
3579 exit(1);
3580 } else if (!S_ISDIR(stat.st_mode)) {
3581 fprintf(stderr, "share path %s is not a directory \n", fse->path);
3582 exit(1);
3585 s->ctx.fs_root = qemu_strdup(fse->path);
3586 len = strlen(conf->tag);
3587 if (len > MAX_TAG_LEN) {
3588 len = MAX_TAG_LEN;
3590 /* s->tag is non-NULL terminated string */
3591 s->tag = qemu_malloc(len);
3592 memcpy(s->tag, conf->tag, len);
3593 s->tag_len = len;
3594 s->ctx.uid = -1;
3596 s->ops = fse->ops;
3597 s->vdev.get_features = virtio_9p_get_features;
3598 s->config_size = sizeof(struct virtio_9p_config) +
3599 s->tag_len;
3600 s->vdev.get_config = virtio_9p_get_config;
3602 return &s->vdev;