virtio-9p: Do not reset atime
[qemu.git] / hw / virtio-9p.c
blobbd6cba9194b9bdd3a711b4c188f48699778c03d7
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, V9fsCreateState *vs, mode_t mode,
164 dev_t dev)
166 FsCred cred;
167 cred_init(&cred);
168 cred.fc_uid = vs->fidp->uid;
169 cred.fc_mode = mode;
170 cred.fc_rdev = dev;
171 return s->ops->mknod(&s->ctx, vs->fullname.data, &cred);
174 static int v9fs_do_mkdir(V9fsState *s, V9fsCreateState *vs)
176 FsCred cred;
178 cred_init(&cred);
179 cred.fc_uid = vs->fidp->uid;
180 cred.fc_mode = vs->perm & 0777;
182 return s->ops->mkdir(&s->ctx, vs->fullname.data, &cred);
185 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
187 return s->ops->fstat(&s->ctx, fd, stbuf);
190 static int v9fs_do_open2(V9fsState *s, V9fsCreateState *vs)
192 FsCred cred;
193 int flags;
195 cred_init(&cred);
196 cred.fc_uid = vs->fidp->uid;
197 cred.fc_mode = vs->perm & 0777;
198 flags = omode_to_uflags(vs->mode) | O_CREAT;
200 return s->ops->open2(&s->ctx, vs->fullname.data, flags, &cred);
203 static int v9fs_do_symlink(V9fsState *s, V9fsCreateState *vs)
205 FsCred cred;
206 cred_init(&cred);
207 cred.fc_uid = vs->fidp->uid;
208 cred.fc_mode = vs->perm | 0777;
210 return s->ops->symlink(&s->ctx, vs->extension.data, vs->fullname.data,
211 &cred);
214 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
216 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
219 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
221 return s->ops->truncate(&s->ctx, path->data, size);
224 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
225 V9fsString *newpath)
227 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
230 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
232 FsCred cred;
233 cred_init(&cred);
234 cred.fc_uid = uid;
235 cred.fc_gid = gid;
237 return s->ops->chown(&s->ctx, path->data, &cred);
240 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path, V9fsStat v9stat)
242 struct timespec ts[2];
244 if (v9stat.atime != -1) {
245 ts[0].tv_sec = v9stat.atime;
246 ts[0].tv_nsec = 0;
247 } else {
248 ts[0].tv_nsec = UTIME_OMIT;
251 if (v9stat.mtime != -1) {
252 ts[1].tv_sec = v9stat.mtime;
253 ts[1].tv_nsec = 0;
254 } else {
255 ts[1].tv_nsec = UTIME_OMIT;
258 return s->ops->utimensat(&s->ctx, path->data, ts);
261 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
263 return s->ops->remove(&s->ctx, path->data);
266 static int v9fs_do_fsync(V9fsState *s, int fd)
268 return s->ops->fsync(&s->ctx, fd);
271 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
273 return s->ops->statfs(&s->ctx, path->data, stbuf);
276 static void v9fs_string_init(V9fsString *str)
278 str->data = NULL;
279 str->size = 0;
282 static void v9fs_string_free(V9fsString *str)
284 qemu_free(str->data);
285 str->data = NULL;
286 str->size = 0;
289 static void v9fs_string_null(V9fsString *str)
291 v9fs_string_free(str);
294 static int number_to_string(void *arg, char type)
296 unsigned int ret = 0;
298 switch (type) {
299 case 'u': {
300 unsigned int num = *(unsigned int *)arg;
302 do {
303 ret++;
304 num = num/10;
305 } while (num);
306 break;
308 default:
309 printf("Number_to_string: Unknown number format\n");
310 return -1;
313 return ret;
316 static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
318 va_list ap2;
319 char *iter = (char *)fmt;
320 int len = 0;
321 int nr_args = 0;
322 char *arg_char_ptr;
323 unsigned int arg_uint;
325 /* Find the number of %'s that denotes an argument */
326 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
327 nr_args++;
328 iter++;
331 len = strlen(fmt) - 2*nr_args;
333 if (!nr_args) {
334 goto alloc_print;
337 va_copy(ap2, ap);
339 iter = (char *)fmt;
341 /* Now parse the format string */
342 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
343 iter++;
344 switch (*iter) {
345 case 'u':
346 arg_uint = va_arg(ap2, unsigned int);
347 len += number_to_string((void *)&arg_uint, 'u');
348 break;
349 case 's':
350 arg_char_ptr = va_arg(ap2, char *);
351 len += strlen(arg_char_ptr);
352 break;
353 case 'c':
354 len += 1;
355 break;
356 default:
357 fprintf(stderr,
358 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
359 return -1;
361 iter++;
364 alloc_print:
365 *strp = qemu_malloc((len + 1) * sizeof(**strp));
367 return vsprintf(*strp, fmt, ap);
370 static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
372 va_list ap;
373 int err;
375 v9fs_string_free(str);
377 va_start(ap, fmt);
378 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
379 BUG_ON(err == -1);
380 va_end(ap);
382 str->size = err;
385 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
387 v9fs_string_free(lhs);
388 v9fs_string_sprintf(lhs, "%s", rhs->data);
391 static size_t v9fs_string_size(V9fsString *str)
393 return str->size;
396 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
398 V9fsFidState *f;
400 for (f = s->fid_list; f; f = f->next) {
401 if (f->fid == fid) {
402 return f;
406 return NULL;
409 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
411 V9fsFidState *f;
413 f = lookup_fid(s, fid);
414 if (f) {
415 return NULL;
418 f = qemu_mallocz(sizeof(V9fsFidState));
420 f->fid = fid;
421 f->fd = -1;
422 f->dir = NULL;
424 f->next = s->fid_list;
425 s->fid_list = f;
427 return f;
430 static int free_fid(V9fsState *s, int32_t fid)
432 V9fsFidState **fidpp, *fidp;
434 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
435 if ((*fidpp)->fid == fid) {
436 break;
440 if (*fidpp == NULL) {
441 return -ENOENT;
444 fidp = *fidpp;
445 *fidpp = fidp->next;
447 if (fidp->fd != -1) {
448 v9fs_do_close(s, fidp->fd);
450 if (fidp->dir) {
451 v9fs_do_closedir(s, fidp->dir);
453 v9fs_string_free(&fidp->path);
454 qemu_free(fidp);
456 return 0;
459 #define P9_QID_TYPE_DIR 0x80
460 #define P9_QID_TYPE_SYMLINK 0x02
462 #define P9_STAT_MODE_DIR 0x80000000
463 #define P9_STAT_MODE_APPEND 0x40000000
464 #define P9_STAT_MODE_EXCL 0x20000000
465 #define P9_STAT_MODE_MOUNT 0x10000000
466 #define P9_STAT_MODE_AUTH 0x08000000
467 #define P9_STAT_MODE_TMP 0x04000000
468 #define P9_STAT_MODE_SYMLINK 0x02000000
469 #define P9_STAT_MODE_LINK 0x01000000
470 #define P9_STAT_MODE_DEVICE 0x00800000
471 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
472 #define P9_STAT_MODE_SOCKET 0x00100000
473 #define P9_STAT_MODE_SETUID 0x00080000
474 #define P9_STAT_MODE_SETGID 0x00040000
475 #define P9_STAT_MODE_SETVTX 0x00010000
477 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
478 P9_STAT_MODE_SYMLINK | \
479 P9_STAT_MODE_LINK | \
480 P9_STAT_MODE_DEVICE | \
481 P9_STAT_MODE_NAMED_PIPE | \
482 P9_STAT_MODE_SOCKET)
484 /* This is the algorithm from ufs in spfs */
485 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
487 size_t size;
489 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
490 memcpy(&qidp->path, &stbuf->st_ino, size);
491 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
492 qidp->type = 0;
493 if (S_ISDIR(stbuf->st_mode)) {
494 qidp->type |= P9_QID_TYPE_DIR;
496 if (S_ISLNK(stbuf->st_mode)) {
497 qidp->type |= P9_QID_TYPE_SYMLINK;
501 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
503 struct stat stbuf;
504 int err;
506 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
507 if (err) {
508 return err;
511 stat_to_qid(&stbuf, qidp);
512 return 0;
515 static V9fsPDU *alloc_pdu(V9fsState *s)
517 V9fsPDU *pdu = NULL;
519 if (!QLIST_EMPTY(&s->free_list)) {
520 pdu = QLIST_FIRST(&s->free_list);
521 QLIST_REMOVE(pdu, next);
523 return pdu;
526 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
528 if (pdu) {
529 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
533 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
534 size_t offset, size_t size, int pack)
536 int i = 0;
537 size_t copied = 0;
539 for (i = 0; size && i < sg_count; i++) {
540 size_t len;
541 if (offset >= sg[i].iov_len) {
542 /* skip this sg */
543 offset -= sg[i].iov_len;
544 continue;
545 } else {
546 len = MIN(sg[i].iov_len - offset, size);
547 if (pack) {
548 memcpy(sg[i].iov_base + offset, addr, len);
549 } else {
550 memcpy(addr, sg[i].iov_base + offset, len);
552 size -= len;
553 copied += len;
554 addr += len;
555 if (size) {
556 offset = 0;
557 continue;
562 return copied;
565 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
567 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
568 offset, size, 0);
571 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
572 size_t size)
574 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
575 offset, size, 1);
578 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
580 size_t pos = 0;
581 int i, j;
582 struct iovec *src_sg;
583 unsigned int num;
585 if (rx) {
586 src_sg = pdu->elem.in_sg;
587 num = pdu->elem.in_num;
588 } else {
589 src_sg = pdu->elem.out_sg;
590 num = pdu->elem.out_num;
593 j = 0;
594 for (i = 0; i < num; i++) {
595 if (offset <= pos) {
596 sg[j].iov_base = src_sg[i].iov_base;
597 sg[j].iov_len = src_sg[i].iov_len;
598 j++;
599 } else if (offset < (src_sg[i].iov_len + pos)) {
600 sg[j].iov_base = src_sg[i].iov_base;
601 sg[j].iov_len = src_sg[i].iov_len;
602 sg[j].iov_base += (offset - pos);
603 sg[j].iov_len -= (offset - pos);
604 j++;
606 pos += src_sg[i].iov_len;
609 return j;
612 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
614 size_t old_offset = offset;
615 va_list ap;
616 int i;
618 va_start(ap, fmt);
619 for (i = 0; fmt[i]; i++) {
620 switch (fmt[i]) {
621 case 'b': {
622 uint8_t *valp = va_arg(ap, uint8_t *);
623 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
624 break;
626 case 'w': {
627 uint16_t val, *valp;
628 valp = va_arg(ap, uint16_t *);
629 val = le16_to_cpupu(valp);
630 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
631 *valp = val;
632 break;
634 case 'd': {
635 uint32_t val, *valp;
636 valp = va_arg(ap, uint32_t *);
637 val = le32_to_cpupu(valp);
638 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
639 *valp = val;
640 break;
642 case 'q': {
643 uint64_t val, *valp;
644 valp = va_arg(ap, uint64_t *);
645 val = le64_to_cpup(valp);
646 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
647 *valp = val;
648 break;
650 case 'v': {
651 struct iovec *iov = va_arg(ap, struct iovec *);
652 int *iovcnt = va_arg(ap, int *);
653 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
654 break;
656 case 's': {
657 V9fsString *str = va_arg(ap, V9fsString *);
658 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
659 /* FIXME: sanity check str->size */
660 str->data = qemu_malloc(str->size + 1);
661 offset += pdu_unpack(str->data, pdu, offset, str->size);
662 str->data[str->size] = 0;
663 break;
665 case 'Q': {
666 V9fsQID *qidp = va_arg(ap, V9fsQID *);
667 offset += pdu_unmarshal(pdu, offset, "bdq",
668 &qidp->type, &qidp->version, &qidp->path);
669 break;
671 case 'S': {
672 V9fsStat *statp = va_arg(ap, V9fsStat *);
673 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
674 &statp->size, &statp->type, &statp->dev,
675 &statp->qid, &statp->mode, &statp->atime,
676 &statp->mtime, &statp->length,
677 &statp->name, &statp->uid, &statp->gid,
678 &statp->muid, &statp->extension,
679 &statp->n_uid, &statp->n_gid,
680 &statp->n_muid);
681 break;
683 default:
684 break;
688 va_end(ap);
690 return offset - old_offset;
693 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
695 size_t old_offset = offset;
696 va_list ap;
697 int i;
699 va_start(ap, fmt);
700 for (i = 0; fmt[i]; i++) {
701 switch (fmt[i]) {
702 case 'b': {
703 uint8_t val = va_arg(ap, int);
704 offset += pdu_pack(pdu, offset, &val, sizeof(val));
705 break;
707 case 'w': {
708 uint16_t val;
709 cpu_to_le16w(&val, va_arg(ap, int));
710 offset += pdu_pack(pdu, offset, &val, sizeof(val));
711 break;
713 case 'd': {
714 uint32_t val;
715 cpu_to_le32w(&val, va_arg(ap, uint32_t));
716 offset += pdu_pack(pdu, offset, &val, sizeof(val));
717 break;
719 case 'q': {
720 uint64_t val;
721 cpu_to_le64w(&val, va_arg(ap, uint64_t));
722 offset += pdu_pack(pdu, offset, &val, sizeof(val));
723 break;
725 case 'v': {
726 struct iovec *iov = va_arg(ap, struct iovec *);
727 int *iovcnt = va_arg(ap, int *);
728 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
729 break;
731 case 's': {
732 V9fsString *str = va_arg(ap, V9fsString *);
733 offset += pdu_marshal(pdu, offset, "w", str->size);
734 offset += pdu_pack(pdu, offset, str->data, str->size);
735 break;
737 case 'Q': {
738 V9fsQID *qidp = va_arg(ap, V9fsQID *);
739 offset += pdu_marshal(pdu, offset, "bdq",
740 qidp->type, qidp->version, qidp->path);
741 break;
743 case 'S': {
744 V9fsStat *statp = va_arg(ap, V9fsStat *);
745 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
746 statp->size, statp->type, statp->dev,
747 &statp->qid, statp->mode, statp->atime,
748 statp->mtime, statp->length, &statp->name,
749 &statp->uid, &statp->gid, &statp->muid,
750 &statp->extension, statp->n_uid,
751 statp->n_gid, statp->n_muid);
752 break;
754 case 'A': {
755 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
756 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
757 statp->st_result_mask,
758 &statp->qid, statp->st_mode,
759 statp->st_uid, statp->st_gid,
760 statp->st_nlink, statp->st_rdev,
761 statp->st_size, statp->st_blksize, statp->st_blocks,
762 statp->st_atime_sec, statp->st_atime_nsec,
763 statp->st_mtime_sec, statp->st_mtime_nsec,
764 statp->st_ctime_sec, statp->st_ctime_nsec,
765 statp->st_btime_sec, statp->st_btime_nsec,
766 statp->st_gen, statp->st_data_version);
767 break;
769 default:
770 break;
773 va_end(ap);
775 return offset - old_offset;
778 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
780 int8_t id = pdu->id + 1; /* Response */
782 if (len < 0) {
783 V9fsString str;
784 int err = -len;
786 str.data = strerror(err);
787 str.size = strlen(str.data);
789 len = 7;
790 len += pdu_marshal(pdu, len, "s", &str);
791 if (dotu) {
792 len += pdu_marshal(pdu, len, "d", err);
795 id = P9_RERROR;
798 /* fill out the header */
799 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
801 /* keep these in sync */
802 pdu->size = len;
803 pdu->id = id;
805 /* push onto queue and notify */
806 virtqueue_push(s->vq, &pdu->elem, len);
808 /* FIXME: we should batch these completions */
809 virtio_notify(&s->vdev, s->vq);
811 free_pdu(s, pdu);
814 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
816 mode_t ret;
818 ret = mode & 0777;
819 if (mode & P9_STAT_MODE_DIR) {
820 ret |= S_IFDIR;
823 if (dotu) {
824 if (mode & P9_STAT_MODE_SYMLINK) {
825 ret |= S_IFLNK;
827 if (mode & P9_STAT_MODE_SOCKET) {
828 ret |= S_IFSOCK;
830 if (mode & P9_STAT_MODE_NAMED_PIPE) {
831 ret |= S_IFIFO;
833 if (mode & P9_STAT_MODE_DEVICE) {
834 if (extension && extension->data[0] == 'c') {
835 ret |= S_IFCHR;
836 } else {
837 ret |= S_IFBLK;
842 if (!(ret&~0777)) {
843 ret |= S_IFREG;
846 if (mode & P9_STAT_MODE_SETUID) {
847 ret |= S_ISUID;
849 if (mode & P9_STAT_MODE_SETGID) {
850 ret |= S_ISGID;
852 if (mode & P9_STAT_MODE_SETVTX) {
853 ret |= S_ISVTX;
856 return ret;
859 static int donttouch_stat(V9fsStat *stat)
861 if (stat->type == -1 &&
862 stat->dev == -1 &&
863 stat->qid.type == -1 &&
864 stat->qid.version == -1 &&
865 stat->qid.path == -1 &&
866 stat->mode == -1 &&
867 stat->atime == -1 &&
868 stat->mtime == -1 &&
869 stat->length == -1 &&
870 !stat->name.size &&
871 !stat->uid.size &&
872 !stat->gid.size &&
873 !stat->muid.size &&
874 stat->n_uid == -1 &&
875 stat->n_gid == -1 &&
876 stat->n_muid == -1) {
877 return 1;
880 return 0;
883 static void v9fs_stat_free(V9fsStat *stat)
885 v9fs_string_free(&stat->name);
886 v9fs_string_free(&stat->uid);
887 v9fs_string_free(&stat->gid);
888 v9fs_string_free(&stat->muid);
889 v9fs_string_free(&stat->extension);
892 static uint32_t stat_to_v9mode(const struct stat *stbuf)
894 uint32_t mode;
896 mode = stbuf->st_mode & 0777;
897 if (S_ISDIR(stbuf->st_mode)) {
898 mode |= P9_STAT_MODE_DIR;
901 if (dotu) {
902 if (S_ISLNK(stbuf->st_mode)) {
903 mode |= P9_STAT_MODE_SYMLINK;
906 if (S_ISSOCK(stbuf->st_mode)) {
907 mode |= P9_STAT_MODE_SOCKET;
910 if (S_ISFIFO(stbuf->st_mode)) {
911 mode |= P9_STAT_MODE_NAMED_PIPE;
914 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
915 mode |= P9_STAT_MODE_DEVICE;
918 if (stbuf->st_mode & S_ISUID) {
919 mode |= P9_STAT_MODE_SETUID;
922 if (stbuf->st_mode & S_ISGID) {
923 mode |= P9_STAT_MODE_SETGID;
926 if (stbuf->st_mode & S_ISVTX) {
927 mode |= P9_STAT_MODE_SETVTX;
931 return mode;
934 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
935 const struct stat *stbuf,
936 V9fsStat *v9stat)
938 int err;
939 const char *str;
941 memset(v9stat, 0, sizeof(*v9stat));
943 stat_to_qid(stbuf, &v9stat->qid);
944 v9stat->mode = stat_to_v9mode(stbuf);
945 v9stat->atime = stbuf->st_atime;
946 v9stat->mtime = stbuf->st_mtime;
947 v9stat->length = stbuf->st_size;
949 v9fs_string_null(&v9stat->uid);
950 v9fs_string_null(&v9stat->gid);
951 v9fs_string_null(&v9stat->muid);
953 if (dotu) {
954 v9stat->n_uid = stbuf->st_uid;
955 v9stat->n_gid = stbuf->st_gid;
956 v9stat->n_muid = 0;
958 v9fs_string_null(&v9stat->extension);
960 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
961 err = v9fs_do_readlink(s, name, &v9stat->extension);
962 if (err == -1) {
963 err = -errno;
964 return err;
966 v9stat->extension.data[err] = 0;
967 v9stat->extension.size = err;
968 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
969 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
970 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
971 major(stbuf->st_rdev), minor(stbuf->st_rdev));
972 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
973 v9fs_string_sprintf(&v9stat->extension, "%s %u",
974 "HARDLINKCOUNT", stbuf->st_nlink);
978 str = strrchr(name->data, '/');
979 if (str) {
980 str += 1;
981 } else {
982 str = name->data;
985 v9fs_string_sprintf(&v9stat->name, "%s", str);
987 v9stat->size = 61 +
988 v9fs_string_size(&v9stat->name) +
989 v9fs_string_size(&v9stat->uid) +
990 v9fs_string_size(&v9stat->gid) +
991 v9fs_string_size(&v9stat->muid) +
992 v9fs_string_size(&v9stat->extension);
993 return 0;
996 #define P9_STATS_MODE 0x00000001ULL
997 #define P9_STATS_NLINK 0x00000002ULL
998 #define P9_STATS_UID 0x00000004ULL
999 #define P9_STATS_GID 0x00000008ULL
1000 #define P9_STATS_RDEV 0x00000010ULL
1001 #define P9_STATS_ATIME 0x00000020ULL
1002 #define P9_STATS_MTIME 0x00000040ULL
1003 #define P9_STATS_CTIME 0x00000080ULL
1004 #define P9_STATS_INO 0x00000100ULL
1005 #define P9_STATS_SIZE 0x00000200ULL
1006 #define P9_STATS_BLOCKS 0x00000400ULL
1008 #define P9_STATS_BTIME 0x00000800ULL
1009 #define P9_STATS_GEN 0x00001000ULL
1010 #define P9_STATS_DATA_VERSION 0x00002000ULL
1012 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1013 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1016 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1017 V9fsStatDotl *v9lstat)
1019 memset(v9lstat, 0, sizeof(*v9lstat));
1021 v9lstat->st_mode = stbuf->st_mode;
1022 v9lstat->st_nlink = stbuf->st_nlink;
1023 v9lstat->st_uid = stbuf->st_uid;
1024 v9lstat->st_gid = stbuf->st_gid;
1025 v9lstat->st_rdev = stbuf->st_rdev;
1026 v9lstat->st_size = stbuf->st_size;
1027 v9lstat->st_blksize = stbuf->st_blksize;
1028 v9lstat->st_blocks = stbuf->st_blocks;
1029 v9lstat->st_atime_sec = stbuf->st_atime;
1030 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1031 v9lstat->st_mtime_sec = stbuf->st_mtime;
1032 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1033 v9lstat->st_ctime_sec = stbuf->st_ctime;
1034 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1035 /* Currently we only support BASIC fields in stat */
1036 v9lstat->st_result_mask = P9_STATS_BASIC;
1038 stat_to_qid(stbuf, &v9lstat->qid);
1041 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1043 while (len && *iovcnt) {
1044 if (len < sg->iov_len) {
1045 sg->iov_len -= len;
1046 sg->iov_base += len;
1047 len = 0;
1048 } else {
1049 len -= sg->iov_len;
1050 sg++;
1051 *iovcnt -= 1;
1055 return sg;
1058 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1060 int i;
1061 int total = 0;
1063 for (i = 0; i < *cnt; i++) {
1064 if ((total + sg[i].iov_len) > cap) {
1065 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1066 i++;
1067 break;
1069 total += sg[i].iov_len;
1072 *cnt = i;
1074 return sg;
1077 static void print_sg(struct iovec *sg, int cnt)
1079 int i;
1081 printf("sg[%d]: {", cnt);
1082 for (i = 0; i < cnt; i++) {
1083 if (i) {
1084 printf(", ");
1086 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1088 printf("}\n");
1091 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1093 V9fsString str;
1094 v9fs_string_init(&str);
1095 v9fs_string_copy(&str, dst);
1096 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1097 v9fs_string_free(&str);
1100 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1102 V9fsString version;
1103 size_t offset = 7;
1105 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1107 if (!strcmp(version.data, "9P2000.u")) {
1108 s->proto_version = V9FS_PROTO_2000U;
1109 } else if (!strcmp(version.data, "9P2000.L")) {
1110 s->proto_version = V9FS_PROTO_2000L;
1111 } else {
1112 v9fs_string_sprintf(&version, "unknown");
1115 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1116 complete_pdu(s, pdu, offset);
1118 v9fs_string_free(&version);
1121 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1123 int32_t fid, afid, n_uname;
1124 V9fsString uname, aname;
1125 V9fsFidState *fidp;
1126 V9fsQID qid;
1127 size_t offset = 7;
1128 ssize_t err;
1130 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1132 fidp = alloc_fid(s, fid);
1133 if (fidp == NULL) {
1134 err = -EINVAL;
1135 goto out;
1138 fidp->uid = n_uname;
1140 v9fs_string_sprintf(&fidp->path, "%s", "/");
1141 err = fid_to_qid(s, fidp, &qid);
1142 if (err) {
1143 err = -EINVAL;
1144 free_fid(s, fid);
1145 goto out;
1148 offset += pdu_marshal(pdu, offset, "Q", &qid);
1150 err = offset;
1151 out:
1152 complete_pdu(s, pdu, err);
1153 v9fs_string_free(&uname);
1154 v9fs_string_free(&aname);
1157 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1159 if (err == -1) {
1160 err = -errno;
1161 goto out;
1164 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1165 if (err) {
1166 goto out;
1168 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1169 err = vs->offset;
1171 out:
1172 complete_pdu(s, vs->pdu, err);
1173 v9fs_stat_free(&vs->v9stat);
1174 qemu_free(vs);
1177 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1179 int32_t fid;
1180 V9fsStatState *vs;
1181 ssize_t err = 0;
1183 vs = qemu_malloc(sizeof(*vs));
1184 vs->pdu = pdu;
1185 vs->offset = 7;
1187 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1189 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1191 vs->fidp = lookup_fid(s, fid);
1192 if (vs->fidp == NULL) {
1193 err = -ENOENT;
1194 goto out;
1197 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1198 v9fs_stat_post_lstat(s, vs, err);
1199 return;
1201 out:
1202 complete_pdu(s, vs->pdu, err);
1203 v9fs_stat_free(&vs->v9stat);
1204 qemu_free(vs);
1207 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1208 int err)
1210 if (err == -1) {
1211 err = -errno;
1212 goto out;
1215 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1216 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1217 err = vs->offset;
1219 out:
1220 complete_pdu(s, vs->pdu, err);
1221 qemu_free(vs);
1224 static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1226 int32_t fid;
1227 V9fsStatStateDotl *vs;
1228 ssize_t err = 0;
1229 V9fsFidState *fidp;
1230 uint64_t request_mask;
1232 vs = qemu_malloc(sizeof(*vs));
1233 vs->pdu = pdu;
1234 vs->offset = 7;
1236 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1238 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1240 fidp = lookup_fid(s, fid);
1241 if (fidp == NULL) {
1242 err = -ENOENT;
1243 goto out;
1246 /* Currently we only support BASIC fields in stat, so there is no
1247 * need to look at request_mask.
1249 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1250 v9fs_getattr_post_lstat(s, vs, err);
1251 return;
1253 out:
1254 complete_pdu(s, vs->pdu, err);
1255 qemu_free(vs);
1258 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1260 complete_pdu(s, vs->pdu, err);
1262 if (vs->nwnames) {
1263 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1264 v9fs_string_free(&vs->wnames[vs->name_idx]);
1267 qemu_free(vs->wnames);
1268 qemu_free(vs->qids);
1272 static void v9fs_walk_marshal(V9fsWalkState *vs)
1274 int i;
1275 vs->offset = 7;
1276 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1278 for (i = 0; i < vs->nwnames; i++) {
1279 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1283 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1284 int err)
1286 if (err == -1) {
1287 free_fid(s, vs->newfidp->fid);
1288 v9fs_string_free(&vs->path);
1289 err = -ENOENT;
1290 goto out;
1293 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1295 vs->name_idx++;
1296 if (vs->name_idx < vs->nwnames) {
1297 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1298 vs->wnames[vs->name_idx].data);
1299 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1301 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1302 v9fs_walk_post_newfid_lstat(s, vs, err);
1303 return;
1306 v9fs_string_free(&vs->path);
1307 v9fs_walk_marshal(vs);
1308 err = vs->offset;
1309 out:
1310 v9fs_walk_complete(s, vs, err);
1313 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1314 int err)
1316 if (err == -1) {
1317 v9fs_string_free(&vs->path);
1318 err = -ENOENT;
1319 goto out;
1322 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1323 vs->name_idx++;
1324 if (vs->name_idx < vs->nwnames) {
1326 v9fs_string_sprintf(&vs->path, "%s/%s",
1327 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1328 v9fs_string_copy(&vs->fidp->path, &vs->path);
1330 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1331 v9fs_walk_post_oldfid_lstat(s, vs, err);
1332 return;
1335 v9fs_string_free(&vs->path);
1336 v9fs_walk_marshal(vs);
1337 err = vs->offset;
1338 out:
1339 v9fs_walk_complete(s, vs, err);
1342 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1344 int32_t fid, newfid;
1345 V9fsWalkState *vs;
1346 int err = 0;
1347 int i;
1349 vs = qemu_malloc(sizeof(*vs));
1350 vs->pdu = pdu;
1351 vs->wnames = NULL;
1352 vs->qids = NULL;
1353 vs->offset = 7;
1355 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1356 &newfid, &vs->nwnames);
1358 if (vs->nwnames) {
1359 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1361 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1363 for (i = 0; i < vs->nwnames; i++) {
1364 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1365 &vs->wnames[i]);
1369 vs->fidp = lookup_fid(s, fid);
1370 if (vs->fidp == NULL) {
1371 err = -ENOENT;
1372 goto out;
1375 /* FIXME: is this really valid? */
1376 if (fid == newfid) {
1378 BUG_ON(vs->fidp->fd != -1);
1379 BUG_ON(vs->fidp->dir);
1380 v9fs_string_init(&vs->path);
1381 vs->name_idx = 0;
1383 if (vs->name_idx < vs->nwnames) {
1384 v9fs_string_sprintf(&vs->path, "%s/%s",
1385 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1386 v9fs_string_copy(&vs->fidp->path, &vs->path);
1388 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1389 v9fs_walk_post_oldfid_lstat(s, vs, err);
1390 return;
1392 } else {
1393 vs->newfidp = alloc_fid(s, newfid);
1394 if (vs->newfidp == NULL) {
1395 err = -EINVAL;
1396 goto out;
1399 vs->newfidp->uid = vs->fidp->uid;
1400 v9fs_string_init(&vs->path);
1401 vs->name_idx = 0;
1402 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1404 if (vs->name_idx < vs->nwnames) {
1405 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1406 vs->wnames[vs->name_idx].data);
1407 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1409 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1410 v9fs_walk_post_newfid_lstat(s, vs, err);
1411 return;
1415 v9fs_walk_marshal(vs);
1416 err = vs->offset;
1417 out:
1418 v9fs_walk_complete(s, vs, err);
1421 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1423 struct statfs stbuf;
1424 int32_t iounit = 0;
1427 * iounit should be multiples of f_bsize (host filesystem block size
1428 * and as well as less than (client msize - P9_IOHDRSZ))
1430 if (!v9fs_do_statfs(s, name, &stbuf)) {
1431 iounit = stbuf.f_bsize;
1432 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1435 if (!iounit) {
1436 iounit = s->msize - P9_IOHDRSZ;
1438 return iounit;
1441 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1443 if (vs->fidp->dir == NULL) {
1444 err = -errno;
1445 goto out;
1448 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1449 err = vs->offset;
1450 out:
1451 complete_pdu(s, vs->pdu, err);
1452 qemu_free(vs);
1456 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1458 int err;
1459 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1460 err = vs->offset;
1461 complete_pdu(s, vs->pdu, err);
1462 qemu_free(vs);
1465 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1467 if (vs->fidp->fd == -1) {
1468 err = -errno;
1469 goto out;
1472 vs->iounit = get_iounit(s, &vs->fidp->path);
1473 v9fs_open_post_getiounit(s, vs);
1474 return;
1475 out:
1476 complete_pdu(s, vs->pdu, err);
1477 qemu_free(vs);
1480 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1482 if (err) {
1483 err = -errno;
1484 goto out;
1487 stat_to_qid(&vs->stbuf, &vs->qid);
1489 if (S_ISDIR(vs->stbuf.st_mode)) {
1490 vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1491 v9fs_open_post_opendir(s, vs, err);
1492 } else {
1493 vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1494 omode_to_uflags(vs->mode));
1495 v9fs_open_post_open(s, vs, err);
1497 return;
1498 out:
1499 complete_pdu(s, vs->pdu, err);
1500 qemu_free(vs);
1503 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1505 int32_t fid;
1506 V9fsOpenState *vs;
1507 ssize_t err = 0;
1510 vs = qemu_malloc(sizeof(*vs));
1511 vs->pdu = pdu;
1512 vs->offset = 7;
1514 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1516 vs->fidp = lookup_fid(s, fid);
1517 if (vs->fidp == NULL) {
1518 err = -ENOENT;
1519 goto out;
1522 BUG_ON(vs->fidp->fd != -1);
1523 BUG_ON(vs->fidp->dir);
1525 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1527 v9fs_open_post_lstat(s, vs, err);
1528 return;
1529 out:
1530 complete_pdu(s, pdu, err);
1531 qemu_free(vs);
1534 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1536 int32_t fid;
1537 size_t offset = 7;
1538 int err;
1540 pdu_unmarshal(pdu, offset, "d", &fid);
1542 err = free_fid(s, fid);
1543 if (err < 0) {
1544 goto out;
1547 offset = 7;
1548 err = offset;
1549 out:
1550 complete_pdu(s, pdu, err);
1553 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1555 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1557 if (err) {
1558 goto out;
1560 v9fs_stat_free(&vs->v9stat);
1561 v9fs_string_free(&vs->name);
1562 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1563 vs->offset += vs->count;
1564 err = vs->offset;
1565 out:
1566 complete_pdu(s, vs->pdu, err);
1567 qemu_free(vs);
1568 return;
1571 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1572 ssize_t err)
1574 if (err) {
1575 err = -errno;
1576 goto out;
1578 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1579 if (err) {
1580 goto out;
1583 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1584 &vs->v9stat);
1585 if ((vs->len != (vs->v9stat.size + 2)) ||
1586 ((vs->count + vs->len) > vs->max_count)) {
1587 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1588 v9fs_read_post_seekdir(s, vs, err);
1589 return;
1591 vs->count += vs->len;
1592 v9fs_stat_free(&vs->v9stat);
1593 v9fs_string_free(&vs->name);
1594 vs->dir_pos = vs->dent->d_off;
1595 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1596 v9fs_read_post_readdir(s, vs, err);
1597 return;
1598 out:
1599 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1600 v9fs_read_post_seekdir(s, vs, err);
1601 return;
1605 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1607 if (vs->dent) {
1608 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1609 v9fs_string_init(&vs->name);
1610 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1611 vs->dent->d_name);
1612 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1613 v9fs_read_post_dir_lstat(s, vs, err);
1614 return;
1617 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1618 vs->offset += vs->count;
1619 err = vs->offset;
1620 complete_pdu(s, vs->pdu, err);
1621 qemu_free(vs);
1622 return;
1625 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1627 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1628 v9fs_read_post_readdir(s, vs, err);
1629 return;
1632 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1633 ssize_t err)
1635 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1636 v9fs_read_post_telldir(s, vs, err);
1637 return;
1640 static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1642 if (err < 0) {
1643 /* IO error return the error */
1644 err = -errno;
1645 goto out;
1647 vs->total += vs->len;
1648 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1649 if (vs->total < vs->count && vs->len > 0) {
1650 do {
1651 if (0) {
1652 print_sg(vs->sg, vs->cnt);
1654 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1655 } while (vs->len == -1 && errno == EINTR);
1656 if (vs->len == -1) {
1657 err = -errno;
1659 v9fs_read_post_readv(s, vs, err);
1660 return;
1662 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1663 vs->offset += vs->count;
1664 err = vs->offset;
1666 out:
1667 complete_pdu(s, vs->pdu, err);
1668 qemu_free(vs);
1671 static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1673 if (err == -1) {
1674 err = -errno;
1675 goto out;
1677 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1679 if (vs->total < vs->count) {
1680 do {
1681 if (0) {
1682 print_sg(vs->sg, vs->cnt);
1684 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1685 } while (vs->len == -1 && errno == EINTR);
1686 if (vs->len == -1) {
1687 err = -errno;
1689 v9fs_read_post_readv(s, vs, err);
1690 return;
1692 out:
1693 complete_pdu(s, vs->pdu, err);
1694 qemu_free(vs);
1697 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1699 int32_t fid;
1700 V9fsReadState *vs;
1701 ssize_t err = 0;
1703 vs = qemu_malloc(sizeof(*vs));
1704 vs->pdu = pdu;
1705 vs->offset = 7;
1706 vs->total = 0;
1707 vs->len = 0;
1708 vs->count = 0;
1710 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1712 vs->fidp = lookup_fid(s, fid);
1713 if (vs->fidp == NULL) {
1714 err = -EINVAL;
1715 goto out;
1718 if (vs->fidp->dir) {
1719 vs->max_count = vs->count;
1720 vs->count = 0;
1721 if (vs->off == 0) {
1722 v9fs_do_rewinddir(s, vs->fidp->dir);
1724 v9fs_read_post_rewinddir(s, vs, err);
1725 return;
1726 } else if (vs->fidp->fd != -1) {
1727 vs->sg = vs->iov;
1728 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1729 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1730 v9fs_read_post_lseek(s, vs, err);
1731 return;
1732 } else {
1733 err = -EINVAL;
1735 out:
1736 complete_pdu(s, pdu, err);
1737 qemu_free(vs);
1740 typedef struct V9fsReadDirState {
1741 V9fsPDU *pdu;
1742 V9fsFidState *fidp;
1743 V9fsQID qid;
1744 off_t saved_dir_pos;
1745 struct dirent *dent;
1746 int32_t count;
1747 int32_t max_count;
1748 size_t offset;
1749 int64_t initial_offset;
1750 V9fsString name;
1751 } V9fsReadDirState;
1753 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
1755 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1756 vs->offset += vs->count;
1757 complete_pdu(s, vs->pdu, vs->offset);
1758 qemu_free(vs);
1759 return;
1762 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
1763 * size of type (1) + size of name.size (2) + strlen(name.data)
1765 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
1767 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
1769 int len;
1770 size_t size;
1772 if (vs->dent) {
1773 v9fs_string_init(&vs->name);
1774 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
1776 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
1777 /* Ran out of buffer. Set dir back to old position and return */
1778 v9fs_do_seekdir(s, vs->fidp->dir, vs->saved_dir_pos);
1779 v9fs_readdir_post_seekdir(s, vs);
1780 return;
1783 /* Fill up just the path field of qid because the client uses
1784 * only that. To fill the entire qid structure we will have
1785 * to stat each dirent found, which is expensive
1787 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
1788 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
1789 /* Fill the other fields with dummy values */
1790 vs->qid.type = 0;
1791 vs->qid.version = 0;
1793 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
1794 &vs->qid, vs->dent->d_off,
1795 vs->dent->d_type, &vs->name);
1796 vs->count += len;
1797 v9fs_string_free(&vs->name);
1798 vs->saved_dir_pos = vs->dent->d_off;
1799 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1800 v9fs_readdir_post_readdir(s, vs);
1801 return;
1804 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1805 vs->offset += vs->count;
1806 complete_pdu(s, vs->pdu, vs->offset);
1807 qemu_free(vs);
1808 return;
1811 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
1813 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1814 v9fs_readdir_post_readdir(s, vs);
1815 return;
1818 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
1820 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1821 v9fs_readdir_post_telldir(s, vs);
1822 return;
1825 static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
1827 int32_t fid;
1828 V9fsReadDirState *vs;
1829 ssize_t err = 0;
1830 size_t offset = 7;
1832 vs = qemu_malloc(sizeof(*vs));
1833 vs->pdu = pdu;
1834 vs->offset = 7;
1835 vs->count = 0;
1837 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
1838 &vs->max_count);
1840 vs->fidp = lookup_fid(s, fid);
1841 if (vs->fidp == NULL || !(vs->fidp->dir)) {
1842 err = -EINVAL;
1843 goto out;
1846 if (vs->initial_offset == 0) {
1847 v9fs_do_rewinddir(s, vs->fidp->dir);
1848 } else {
1849 v9fs_do_seekdir(s, vs->fidp->dir, vs->initial_offset);
1852 v9fs_readdir_post_setdir(s, vs);
1853 return;
1855 out:
1856 complete_pdu(s, pdu, err);
1857 qemu_free(vs);
1858 return;
1861 static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1862 ssize_t err)
1864 if (err < 0) {
1865 /* IO error return the error */
1866 err = -errno;
1867 goto out;
1869 vs->total += vs->len;
1870 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1871 if (vs->total < vs->count && vs->len > 0) {
1872 do {
1873 if (0) {
1874 print_sg(vs->sg, vs->cnt);
1876 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1877 } while (vs->len == -1 && errno == EINTR);
1878 if (vs->len == -1) {
1879 err = -errno;
1881 v9fs_write_post_writev(s, vs, err);
1882 return;
1884 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1886 err = vs->offset;
1887 out:
1888 complete_pdu(s, vs->pdu, err);
1889 qemu_free(vs);
1892 static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1894 if (err == -1) {
1895 err = -errno;
1896 goto out;
1898 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1900 if (vs->total < vs->count) {
1901 do {
1902 if (0) {
1903 print_sg(vs->sg, vs->cnt);
1905 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1906 } while (vs->len == -1 && errno == EINTR);
1907 if (vs->len == -1) {
1908 err = -errno;
1910 v9fs_write_post_writev(s, vs, err);
1911 return;
1914 out:
1915 complete_pdu(s, vs->pdu, err);
1916 qemu_free(vs);
1919 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1921 int32_t fid;
1922 V9fsWriteState *vs;
1923 ssize_t err;
1925 vs = qemu_malloc(sizeof(*vs));
1927 vs->pdu = pdu;
1928 vs->offset = 7;
1929 vs->sg = vs->iov;
1930 vs->total = 0;
1931 vs->len = 0;
1933 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1934 vs->sg, &vs->cnt);
1936 vs->fidp = lookup_fid(s, fid);
1937 if (vs->fidp == NULL) {
1938 err = -EINVAL;
1939 goto out;
1942 if (vs->fidp->fd == -1) {
1943 err = -EINVAL;
1944 goto out;
1947 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1949 v9fs_write_post_lseek(s, vs, err);
1950 return;
1952 out:
1953 complete_pdu(s, vs->pdu, err);
1954 qemu_free(vs);
1957 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
1959 int err;
1960 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1961 stat_to_qid(&vs->stbuf, &vs->qid);
1963 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1964 err = vs->offset;
1966 complete_pdu(s, vs->pdu, err);
1967 v9fs_string_free(&vs->name);
1968 v9fs_string_free(&vs->extension);
1969 v9fs_string_free(&vs->fullname);
1970 qemu_free(vs);
1973 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1975 if (err == 0) {
1976 vs->iounit = get_iounit(s, &vs->fidp->path);
1977 v9fs_create_post_getiounit(s, vs);
1978 return;
1981 complete_pdu(s, vs->pdu, err);
1982 v9fs_string_free(&vs->name);
1983 v9fs_string_free(&vs->extension);
1984 v9fs_string_free(&vs->fullname);
1985 qemu_free(vs);
1988 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1990 if (err) {
1991 err = -errno;
1993 v9fs_post_create(s, vs, err);
1996 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1997 int err)
1999 if (!vs->fidp->dir) {
2000 err = -errno;
2002 v9fs_post_create(s, vs, err);
2005 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2006 int err)
2008 if (err) {
2009 err = -errno;
2010 goto out;
2013 vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
2014 v9fs_create_post_opendir(s, vs, err);
2015 return;
2017 out:
2018 v9fs_post_create(s, vs, err);
2021 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2023 if (err) {
2024 err = -errno;
2025 goto out;
2028 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2029 v9fs_create_post_dir_lstat(s, vs, err);
2030 return;
2032 out:
2033 v9fs_post_create(s, vs, err);
2036 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2038 if (err) {
2039 vs->fidp->fd = -1;
2040 err = -errno;
2043 v9fs_post_create(s, vs, err);
2044 return;
2047 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2049 if (vs->fidp->fd == -1) {
2050 err = -errno;
2051 goto out;
2054 err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
2055 v9fs_create_post_fstat(s, vs, err);
2057 return;
2059 out:
2060 v9fs_post_create(s, vs, err);
2064 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2067 if (err == 0 || errno != ENOENT) {
2068 err = -errno;
2069 goto out;
2072 if (vs->perm & P9_STAT_MODE_DIR) {
2073 err = v9fs_do_mkdir(s, vs);
2074 v9fs_create_post_mkdir(s, vs, err);
2075 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2076 err = v9fs_do_symlink(s, vs);
2077 v9fs_create_post_perms(s, vs, err);
2078 } else if (vs->perm & P9_STAT_MODE_LINK) {
2079 int32_t nfid = atoi(vs->extension.data);
2080 V9fsFidState *nfidp = lookup_fid(s, nfid);
2081 if (nfidp == NULL) {
2082 err = -errno;
2083 v9fs_post_create(s, vs, err);
2085 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2086 v9fs_create_post_perms(s, vs, err);
2087 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2088 char ctype;
2089 uint32_t major, minor;
2090 mode_t nmode = 0;
2092 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2093 &minor) != 3) {
2094 err = -errno;
2095 v9fs_post_create(s, vs, err);
2098 switch (ctype) {
2099 case 'c':
2100 nmode = S_IFCHR;
2101 break;
2102 case 'b':
2103 nmode = S_IFBLK;
2104 break;
2105 default:
2106 err = -EIO;
2107 v9fs_post_create(s, vs, err);
2110 nmode |= vs->perm & 0777;
2111 err = v9fs_do_mknod(s, vs, nmode, makedev(major, minor));
2112 v9fs_create_post_perms(s, vs, err);
2113 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2114 err = v9fs_do_mknod(s, vs, S_IFIFO | (vs->perm & 0777), 0);
2115 v9fs_post_create(s, vs, err);
2116 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2117 err = v9fs_do_mknod(s, vs, S_IFSOCK | (vs->perm & 0777), 0);
2118 v9fs_post_create(s, vs, err);
2119 } else {
2120 vs->fidp->fd = v9fs_do_open2(s, vs);
2121 v9fs_create_post_open2(s, vs, err);
2124 return;
2126 out:
2127 v9fs_post_create(s, vs, err);
2130 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2132 int32_t fid;
2133 V9fsCreateState *vs;
2134 int err = 0;
2136 vs = qemu_malloc(sizeof(*vs));
2137 vs->pdu = pdu;
2138 vs->offset = 7;
2140 v9fs_string_init(&vs->fullname);
2142 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2143 &vs->perm, &vs->mode, &vs->extension);
2145 vs->fidp = lookup_fid(s, fid);
2146 if (vs->fidp == NULL) {
2147 err = -EINVAL;
2148 goto out;
2151 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2152 vs->name.data);
2154 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2155 v9fs_create_post_lstat(s, vs, err);
2156 return;
2158 out:
2159 complete_pdu(s, vs->pdu, err);
2160 v9fs_string_free(&vs->name);
2161 v9fs_string_free(&vs->extension);
2162 qemu_free(vs);
2165 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2167 /* A nop call with no return */
2168 complete_pdu(s, pdu, 7);
2171 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2172 int err)
2174 if (err < 0) {
2175 err = -errno;
2176 } else {
2177 err = vs->offset;
2180 /* For TREMOVE we need to clunk the fid even on failed remove */
2181 free_fid(s, vs->fidp->fid);
2183 complete_pdu(s, vs->pdu, err);
2184 qemu_free(vs);
2187 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2189 int32_t fid;
2190 V9fsRemoveState *vs;
2191 int err = 0;
2193 vs = qemu_malloc(sizeof(*vs));
2194 vs->pdu = pdu;
2195 vs->offset = 7;
2197 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2199 vs->fidp = lookup_fid(s, fid);
2200 if (vs->fidp == NULL) {
2201 err = -EINVAL;
2202 goto out;
2205 err = v9fs_do_remove(s, &vs->fidp->path);
2206 v9fs_remove_post_remove(s, vs, err);
2207 return;
2209 out:
2210 complete_pdu(s, pdu, err);
2211 qemu_free(vs);
2214 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2216 if (err < 0) {
2217 goto out;
2220 err = vs->offset;
2222 out:
2223 v9fs_stat_free(&vs->v9stat);
2224 complete_pdu(s, vs->pdu, err);
2225 qemu_free(vs);
2228 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2230 if (err < 0) {
2231 goto out;
2234 if (vs->v9stat.name.size != 0) {
2235 v9fs_string_free(&vs->nname);
2238 if (vs->v9stat.length != -1) {
2239 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2240 err = -errno;
2243 v9fs_wstat_post_truncate(s, vs, err);
2244 return;
2246 out:
2247 v9fs_stat_free(&vs->v9stat);
2248 complete_pdu(s, vs->pdu, err);
2249 qemu_free(vs);
2252 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2254 V9fsFidState *fidp;
2255 if (err < 0) {
2256 goto out;
2259 if (vs->v9stat.name.size != 0) {
2260 char *old_name, *new_name;
2261 char *end;
2263 old_name = vs->fidp->path.data;
2264 end = strrchr(old_name, '/');
2265 if (end) {
2266 end++;
2267 } else {
2268 end = old_name;
2271 new_name = qemu_mallocz(end - old_name + vs->v9stat.name.size + 1);
2273 memcpy(new_name, old_name, end - old_name);
2274 memcpy(new_name + (end - old_name), vs->v9stat.name.data,
2275 vs->v9stat.name.size);
2276 vs->nname.data = new_name;
2277 vs->nname.size = strlen(new_name);
2279 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2280 if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2281 err = -errno;
2282 } else {
2284 * Fixup fid's pointing to the old name to
2285 * start pointing to the new name
2287 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2289 if (vs->fidp == fidp) {
2291 * we replace name of this fid towards the end
2292 * so that our below strcmp will work
2294 continue;
2296 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2297 strlen(vs->fidp->path.data))) {
2298 /* replace the name */
2299 v9fs_fix_path(&fidp->path, &vs->nname,
2300 strlen(vs->fidp->path.data));
2303 v9fs_string_copy(&vs->fidp->path, &vs->nname);
2307 v9fs_wstat_post_rename(s, vs, err);
2308 return;
2310 out:
2311 v9fs_stat_free(&vs->v9stat);
2312 complete_pdu(s, vs->pdu, err);
2313 qemu_free(vs);
2316 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2318 if (err < 0) {
2319 goto out;
2322 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2323 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2324 vs->v9stat.n_gid)) {
2325 err = -errno;
2328 v9fs_wstat_post_chown(s, vs, err);
2329 return;
2331 out:
2332 v9fs_stat_free(&vs->v9stat);
2333 complete_pdu(s, vs->pdu, err);
2334 qemu_free(vs);
2337 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2339 if (err < 0) {
2340 goto out;
2343 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2344 if (v9fs_do_utimensat(s, &vs->fidp->path, vs->v9stat)) {
2345 err = -errno;
2349 v9fs_wstat_post_utime(s, vs, err);
2350 return;
2352 out:
2353 v9fs_stat_free(&vs->v9stat);
2354 complete_pdu(s, vs->pdu, err);
2355 qemu_free(vs);
2358 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2360 if (err == -1) {
2361 err = -errno;
2363 v9fs_stat_free(&vs->v9stat);
2364 complete_pdu(s, vs->pdu, err);
2365 qemu_free(vs);
2368 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2370 uint32_t v9_mode;
2372 if (err == -1) {
2373 err = -errno;
2374 goto out;
2377 v9_mode = stat_to_v9mode(&vs->stbuf);
2379 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2380 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2381 /* Attempting to change the type */
2382 err = -EIO;
2383 goto out;
2386 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2387 &vs->v9stat.extension))) {
2388 err = -errno;
2390 v9fs_wstat_post_chmod(s, vs, err);
2391 return;
2393 out:
2394 v9fs_stat_free(&vs->v9stat);
2395 complete_pdu(s, vs->pdu, err);
2396 qemu_free(vs);
2399 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2401 int32_t fid;
2402 V9fsWstatState *vs;
2403 int err = 0;
2405 vs = qemu_malloc(sizeof(*vs));
2406 vs->pdu = pdu;
2407 vs->offset = 7;
2409 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2411 vs->fidp = lookup_fid(s, fid);
2412 if (vs->fidp == NULL) {
2413 err = -EINVAL;
2414 goto out;
2417 /* do we need to sync the file? */
2418 if (donttouch_stat(&vs->v9stat)) {
2419 err = v9fs_do_fsync(s, vs->fidp->fd);
2420 v9fs_wstat_post_fsync(s, vs, err);
2421 return;
2424 if (vs->v9stat.mode != -1) {
2425 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2426 v9fs_wstat_post_lstat(s, vs, err);
2427 return;
2430 v9fs_wstat_post_chmod(s, vs, err);
2431 return;
2433 out:
2434 v9fs_stat_free(&vs->v9stat);
2435 complete_pdu(s, vs->pdu, err);
2436 qemu_free(vs);
2439 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
2441 int32_t bsize_factor;
2443 if (err) {
2444 err = -errno;
2445 goto out;
2449 * compute bsize factor based on host file system block size
2450 * and client msize
2452 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
2453 if (!bsize_factor) {
2454 bsize_factor = 1;
2456 vs->v9statfs.f_type = vs->stbuf.f_type;
2457 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
2458 vs->v9statfs.f_bsize *= bsize_factor;
2460 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2461 * adjust(divide) the number of blocks, free blocks and available
2462 * blocks by bsize factor
2464 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
2465 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
2466 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
2467 vs->v9statfs.f_files = vs->stbuf.f_files;
2468 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
2469 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
2470 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
2471 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
2473 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
2474 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
2475 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
2476 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
2477 vs->v9statfs.f_namelen);
2479 out:
2480 complete_pdu(s, vs->pdu, vs->offset);
2481 qemu_free(vs);
2484 static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
2486 V9fsStatfsState *vs;
2487 ssize_t err = 0;
2489 vs = qemu_malloc(sizeof(*vs));
2490 vs->pdu = pdu;
2491 vs->offset = 7;
2493 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
2495 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
2497 vs->fidp = lookup_fid(s, vs->fid);
2498 if (vs->fidp == NULL) {
2499 err = -ENOENT;
2500 goto out;
2503 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
2504 v9fs_statfs_post_statfs(s, vs, err);
2505 return;
2507 out:
2508 complete_pdu(s, vs->pdu, err);
2509 qemu_free(vs);
2512 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2514 static pdu_handler_t *pdu_handlers[] = {
2515 [P9_TREADDIR] = v9fs_readdir,
2516 [P9_TSTATFS] = v9fs_statfs,
2517 [P9_TGETATTR] = v9fs_getattr,
2518 [P9_TVERSION] = v9fs_version,
2519 [P9_TATTACH] = v9fs_attach,
2520 [P9_TSTAT] = v9fs_stat,
2521 [P9_TWALK] = v9fs_walk,
2522 [P9_TCLUNK] = v9fs_clunk,
2523 [P9_TOPEN] = v9fs_open,
2524 [P9_TREAD] = v9fs_read,
2525 #if 0
2526 [P9_TAUTH] = v9fs_auth,
2527 #endif
2528 [P9_TFLUSH] = v9fs_flush,
2529 [P9_TCREATE] = v9fs_create,
2530 [P9_TWRITE] = v9fs_write,
2531 [P9_TWSTAT] = v9fs_wstat,
2532 [P9_TREMOVE] = v9fs_remove,
2535 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2537 pdu_handler_t *handler;
2539 if (debug_9p_pdu) {
2540 pprint_pdu(pdu);
2543 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2545 handler = pdu_handlers[pdu->id];
2546 BUG_ON(handler == NULL);
2548 handler(s, pdu);
2551 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2553 V9fsState *s = (V9fsState *)vdev;
2554 V9fsPDU *pdu;
2555 ssize_t len;
2557 while ((pdu = alloc_pdu(s)) &&
2558 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2559 uint8_t *ptr;
2561 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2562 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2564 ptr = pdu->elem.out_sg[0].iov_base;
2566 memcpy(&pdu->size, ptr, 4);
2567 pdu->id = ptr[4];
2568 memcpy(&pdu->tag, ptr + 5, 2);
2570 submit_pdu(s, pdu);
2573 free_pdu(s, pdu);
2576 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2578 features |= 1 << VIRTIO_9P_MOUNT_TAG;
2579 return features;
2582 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2584 return (V9fsState *)vdev;
2587 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2589 struct virtio_9p_config *cfg;
2590 V9fsState *s = to_virtio_9p(vdev);
2592 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2593 s->tag_len);
2594 stw_raw(&cfg->tag_len, s->tag_len);
2595 memcpy(cfg->tag, s->tag, s->tag_len);
2596 memcpy(config, cfg, s->config_size);
2597 qemu_free(cfg);
2600 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2602 V9fsState *s;
2603 int i, len;
2604 struct stat stat;
2605 FsTypeEntry *fse;
2608 s = (V9fsState *)virtio_common_init("virtio-9p",
2609 VIRTIO_ID_9P,
2610 sizeof(struct virtio_9p_config)+
2611 MAX_TAG_LEN,
2612 sizeof(V9fsState));
2614 /* initialize pdu allocator */
2615 QLIST_INIT(&s->free_list);
2616 for (i = 0; i < (MAX_REQ - 1); i++) {
2617 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2620 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2622 fse = get_fsdev_fsentry(conf->fsdev_id);
2624 if (!fse) {
2625 /* We don't have a fsdev identified by fsdev_id */
2626 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2627 "with the id %s\n", conf->fsdev_id);
2628 exit(1);
2631 if (!fse->path || !conf->tag) {
2632 /* we haven't specified a mount_tag or the path */
2633 fprintf(stderr, "fsdev with id %s needs path "
2634 "and Virtio-9p device needs mount_tag arguments\n",
2635 conf->fsdev_id);
2636 exit(1);
2639 if (!strcmp(fse->security_model, "passthrough")) {
2640 /* Files on the Fileserver set to client user credentials */
2641 s->ctx.fs_sm = SM_PASSTHROUGH;
2642 } else if (!strcmp(fse->security_model, "mapped")) {
2643 /* Files on the fileserver are set to QEMU credentials.
2644 * Client user credentials are saved in extended attributes.
2646 s->ctx.fs_sm = SM_MAPPED;
2647 } else {
2648 /* user haven't specified a correct security option */
2649 fprintf(stderr, "one of the following must be specified as the"
2650 "security option:\n\t security_model=passthrough \n\t "
2651 "security_model=mapped\n");
2652 return NULL;
2655 if (lstat(fse->path, &stat)) {
2656 fprintf(stderr, "share path %s does not exist\n", fse->path);
2657 exit(1);
2658 } else if (!S_ISDIR(stat.st_mode)) {
2659 fprintf(stderr, "share path %s is not a directory \n", fse->path);
2660 exit(1);
2663 s->ctx.fs_root = qemu_strdup(fse->path);
2664 len = strlen(conf->tag);
2665 if (len > MAX_TAG_LEN) {
2666 len = MAX_TAG_LEN;
2668 /* s->tag is non-NULL terminated string */
2669 s->tag = qemu_malloc(len);
2670 memcpy(s->tag, conf->tag, len);
2671 s->tag_len = len;
2672 s->ctx.uid = -1;
2674 s->ops = fse->ops;
2675 s->vdev.get_features = virtio_9p_get_features;
2676 s->config_size = sizeof(struct virtio_9p_config) +
2677 s->tag_len;
2678 s->vdev.get_config = virtio_9p_get_config;
2680 return &s->vdev;