]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-9p.c
virtio-9p: Add P9_TWALK support
[mirror_qemu.git] / hw / virtio-9p.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
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"
20
21int dotu = 1;
22int debug_9p_pdu;
23
131dcb25
AL
24static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
25{
26 return s->ops->lstat(&s->ctx, path->data, stbuf);
27}
28
29static int v9fs_do_setuid(V9fsState *s, uid_t uid)
30{
31 return s->ops->setuid(&s->ctx, uid);
32}
33
34static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
35{
36 ssize_t len;
37
38 buf->data = qemu_malloc(1024);
39
40 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
41 if (len > -1) {
42 buf->size = len;
43 buf->data[len] = 0;
44 }
45
46 return len;
47}
48
49static int v9fs_do_close(V9fsState *s, int fd)
50{
51 return s->ops->close(&s->ctx, fd);
52}
53
54static int v9fs_do_closedir(V9fsState *s, DIR *dir)
55{
56 return s->ops->closedir(&s->ctx, dir);
57}
58
a03f7874
AL
59static void v9fs_string_init(V9fsString *str)
60{
61 str->data = NULL;
62 str->size = 0;
63}
64
65static void v9fs_string_free(V9fsString *str)
66{
67 qemu_free(str->data);
68 str->data = NULL;
69 str->size = 0;
70}
71
72static void v9fs_string_null(V9fsString *str)
73{
74 v9fs_string_free(str);
75}
76
77static int number_to_string(void *arg, char type)
78{
79 unsigned int ret = 0;
80
81 switch (type) {
82 case 'u': {
83 unsigned int num = *(unsigned int *)arg;
84
85 do {
86 ret++;
87 num = num/10;
88 } while (num);
89 break;
90 }
91 default:
92 printf("Number_to_string: Unknown number format\n");
93 return -1;
94 }
95
96 return ret;
97}
98
99static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
100{
101 va_list ap2;
102 char *iter = (char *)fmt;
103 int len = 0;
104 int nr_args = 0;
105 char *arg_char_ptr;
106 unsigned int arg_uint;
107
108 /* Find the number of %'s that denotes an argument */
109 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
110 nr_args++;
111 iter++;
112 }
113
114 len = strlen(fmt) - 2*nr_args;
115
116 if (!nr_args) {
117 goto alloc_print;
118 }
119
120 va_copy(ap2, ap);
121
122 iter = (char *)fmt;
123
124 /* Now parse the format string */
125 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
126 iter++;
127 switch (*iter) {
128 case 'u':
129 arg_uint = va_arg(ap2, unsigned int);
130 len += number_to_string((void *)&arg_uint, 'u');
131 break;
132 case 's':
133 arg_char_ptr = va_arg(ap2, char *);
134 len += strlen(arg_char_ptr);
135 break;
136 case 'c':
137 len += 1;
138 break;
139 default:
140 fprintf(stderr,
141 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
142 return -1;
143 }
144 iter++;
145 }
146
147alloc_print:
148 *strp = qemu_malloc((len + 1) * sizeof(**strp));
149
150 return vsprintf(*strp, fmt, ap);
151}
152
153static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
154{
155 va_list ap;
156 int err;
157
158 v9fs_string_free(str);
159
160 va_start(ap, fmt);
161 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
162 BUG_ON(err == -1);
163 va_end(ap);
164
165 str->size = err;
166}
167
168static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
169{
170 v9fs_string_free(lhs);
171 v9fs_string_sprintf(lhs, "%s", rhs->data);
172}
173
174static size_t v9fs_string_size(V9fsString *str)
175{
176 return str->size;
177}
178
286d5652
AL
179static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
180{
181 V9fsFidState *f;
182
183 for (f = s->fid_list; f; f = f->next) {
184 if (f->fid == fid) {
185 v9fs_do_setuid(s, f->uid);
186 return f;
187 }
188 }
189
190 return NULL;
191}
192
193static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
194{
195 V9fsFidState *f;
196
197 f = lookup_fid(s, fid);
198 if (f) {
199 return NULL;
200 }
201
202 f = qemu_mallocz(sizeof(V9fsFidState));
203
204 f->fid = fid;
205 f->fd = -1;
206 f->dir = NULL;
207
208 f->next = s->fid_list;
209 s->fid_list = f;
210
211 return f;
212}
213
214static int free_fid(V9fsState *s, int32_t fid)
215{
216 V9fsFidState **fidpp, *fidp;
217
218 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
219 if ((*fidpp)->fid == fid) {
220 break;
221 }
222 }
223
224 if (*fidpp == NULL) {
225 return -ENOENT;
226 }
227
228 fidp = *fidpp;
229 *fidpp = fidp->next;
230
231 if (fidp->fd != -1) {
232 v9fs_do_close(s, fidp->fd);
233 }
234 if (fidp->dir) {
235 v9fs_do_closedir(s, fidp->dir);
236 }
237 v9fs_string_free(&fidp->path);
238 qemu_free(fidp);
239
240 return 0;
241}
242
243#define P9_QID_TYPE_DIR 0x80
244#define P9_QID_TYPE_SYMLINK 0x02
245
246#define P9_STAT_MODE_DIR 0x80000000
247#define P9_STAT_MODE_APPEND 0x40000000
248#define P9_STAT_MODE_EXCL 0x20000000
249#define P9_STAT_MODE_MOUNT 0x10000000
250#define P9_STAT_MODE_AUTH 0x08000000
251#define P9_STAT_MODE_TMP 0x04000000
252#define P9_STAT_MODE_SYMLINK 0x02000000
253#define P9_STAT_MODE_LINK 0x01000000
254#define P9_STAT_MODE_DEVICE 0x00800000
255#define P9_STAT_MODE_NAMED_PIPE 0x00200000
256#define P9_STAT_MODE_SOCKET 0x00100000
257#define P9_STAT_MODE_SETUID 0x00080000
258#define P9_STAT_MODE_SETGID 0x00040000
259#define P9_STAT_MODE_SETVTX 0x00010000
260
261#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
262 P9_STAT_MODE_SYMLINK | \
263 P9_STAT_MODE_LINK | \
264 P9_STAT_MODE_DEVICE | \
265 P9_STAT_MODE_NAMED_PIPE | \
266 P9_STAT_MODE_SOCKET)
267
268/* This is the algorithm from ufs in spfs */
269static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
270{
271 size_t size;
272
273 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
274 memcpy(&qidp->path, &stbuf->st_ino, size);
275 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
276 qidp->type = 0;
277 if (S_ISDIR(stbuf->st_mode)) {
278 qidp->type |= P9_QID_TYPE_DIR;
279 }
280 if (S_ISLNK(stbuf->st_mode)) {
281 qidp->type |= P9_QID_TYPE_SYMLINK;
282 }
283}
284
285static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
286{
287 struct stat stbuf;
288 int err;
289
290 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
291 if (err) {
292 return err;
293 }
294
295 stat_to_qid(&stbuf, qidp);
296 return 0;
297}
298
9f107513
AL
299static V9fsPDU *alloc_pdu(V9fsState *s)
300{
301 V9fsPDU *pdu = NULL;
302
303 if (!QLIST_EMPTY(&s->free_list)) {
304 pdu = QLIST_FIRST(&s->free_list);
305 QLIST_REMOVE(pdu, next);
306 }
307 return pdu;
308}
309
310static void free_pdu(V9fsState *s, V9fsPDU *pdu)
311{
312 if (pdu) {
313 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
314 }
315}
316
317size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
318 size_t offset, size_t size, int pack)
319{
320 int i = 0;
321 size_t copied = 0;
322
323 for (i = 0; size && i < sg_count; i++) {
324 size_t len;
325 if (offset >= sg[i].iov_len) {
326 /* skip this sg */
327 offset -= sg[i].iov_len;
328 continue;
329 } else {
330 len = MIN(sg[i].iov_len - offset, size);
331 if (pack) {
332 memcpy(sg[i].iov_base + offset, addr, len);
333 } else {
334 memcpy(addr, sg[i].iov_base + offset, len);
335 }
336 size -= len;
337 copied += len;
338 addr += len;
339 if (size) {
340 offset = 0;
341 continue;
342 }
343 }
344 }
345
346 return copied;
347}
348
405a549a
AL
349static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
350{
351 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
352 offset, size, 0);
353}
354
355static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
356 size_t size)
357{
358 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
359 offset, size, 1);
360}
361
362static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
363{
364 size_t pos = 0;
365 int i, j;
366 struct iovec *src_sg;
367 unsigned int num;
368
369 if (rx) {
370 src_sg = pdu->elem.in_sg;
371 num = pdu->elem.in_num;
372 } else {
373 src_sg = pdu->elem.out_sg;
374 num = pdu->elem.out_num;
375 }
376
377 j = 0;
378 for (i = 0; i < num; i++) {
379 if (offset <= pos) {
380 sg[j].iov_base = src_sg[i].iov_base;
381 sg[j].iov_len = src_sg[i].iov_len;
382 j++;
383 } else if (offset < (src_sg[i].iov_len + pos)) {
384 sg[j].iov_base = src_sg[i].iov_base;
385 sg[j].iov_len = src_sg[i].iov_len;
386 sg[j].iov_base += (offset - pos);
387 sg[j].iov_len -= (offset - pos);
388 j++;
389 }
390 pos += src_sg[i].iov_len;
391 }
392
393 return j;
394}
395
396static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
397{
398 size_t old_offset = offset;
399 va_list ap;
400 int i;
401
402 va_start(ap, fmt);
403 for (i = 0; fmt[i]; i++) {
404 switch (fmt[i]) {
405 case 'b': {
406 uint8_t *valp = va_arg(ap, uint8_t *);
407 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
408 break;
409 }
410 case 'w': {
411 uint16_t val, *valp;
412 valp = va_arg(ap, uint16_t *);
413 val = le16_to_cpupu(valp);
414 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
415 *valp = val;
416 break;
417 }
418 case 'd': {
419 uint32_t val, *valp;
420 valp = va_arg(ap, uint32_t *);
421 val = le32_to_cpupu(valp);
422 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
423 *valp = val;
424 break;
425 }
426 case 'q': {
427 uint64_t val, *valp;
428 valp = va_arg(ap, uint64_t *);
429 val = le64_to_cpup(valp);
430 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
431 *valp = val;
432 break;
433 }
434 case 'v': {
435 struct iovec *iov = va_arg(ap, struct iovec *);
436 int *iovcnt = va_arg(ap, int *);
437 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
438 break;
439 }
440 case 's': {
441 V9fsString *str = va_arg(ap, V9fsString *);
442 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
443 /* FIXME: sanity check str->size */
444 str->data = qemu_malloc(str->size + 1);
445 offset += pdu_unpack(str->data, pdu, offset, str->size);
446 str->data[str->size] = 0;
447 break;
448 }
449 case 'Q': {
450 V9fsQID *qidp = va_arg(ap, V9fsQID *);
451 offset += pdu_unmarshal(pdu, offset, "bdq",
452 &qidp->type, &qidp->version, &qidp->path);
453 break;
454 }
455 case 'S': {
456 V9fsStat *statp = va_arg(ap, V9fsStat *);
457 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
458 &statp->size, &statp->type, &statp->dev,
459 &statp->qid, &statp->mode, &statp->atime,
460 &statp->mtime, &statp->length,
461 &statp->name, &statp->uid, &statp->gid,
462 &statp->muid, &statp->extension,
463 &statp->n_uid, &statp->n_gid,
464 &statp->n_muid);
465 break;
466 }
467 default:
468 break;
469 }
470 }
471
472 va_end(ap);
473
474 return offset - old_offset;
475}
476
477static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
478{
479 size_t old_offset = offset;
480 va_list ap;
481 int i;
482
483 va_start(ap, fmt);
484 for (i = 0; fmt[i]; i++) {
485 switch (fmt[i]) {
486 case 'b': {
487 uint8_t val = va_arg(ap, int);
488 offset += pdu_pack(pdu, offset, &val, sizeof(val));
489 break;
490 }
491 case 'w': {
492 uint16_t val;
493 cpu_to_le16w(&val, va_arg(ap, int));
494 offset += pdu_pack(pdu, offset, &val, sizeof(val));
495 break;
496 }
497 case 'd': {
498 uint32_t val;
499 cpu_to_le32w(&val, va_arg(ap, uint32_t));
500 offset += pdu_pack(pdu, offset, &val, sizeof(val));
501 break;
502 }
503 case 'q': {
504 uint64_t val;
505 cpu_to_le64w(&val, va_arg(ap, uint64_t));
506 offset += pdu_pack(pdu, offset, &val, sizeof(val));
507 break;
508 }
509 case 'v': {
510 struct iovec *iov = va_arg(ap, struct iovec *);
511 int *iovcnt = va_arg(ap, int *);
512 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
513 break;
514 }
515 case 's': {
516 V9fsString *str = va_arg(ap, V9fsString *);
517 offset += pdu_marshal(pdu, offset, "w", str->size);
518 offset += pdu_pack(pdu, offset, str->data, str->size);
519 break;
520 }
521 case 'Q': {
522 V9fsQID *qidp = va_arg(ap, V9fsQID *);
523 offset += pdu_marshal(pdu, offset, "bdq",
524 qidp->type, qidp->version, qidp->path);
525 break;
526 }
527 case 'S': {
528 V9fsStat *statp = va_arg(ap, V9fsStat *);
529 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
530 statp->size, statp->type, statp->dev,
531 &statp->qid, statp->mode, statp->atime,
532 statp->mtime, statp->length, &statp->name,
533 &statp->uid, &statp->gid, &statp->muid,
534 &statp->extension, statp->n_uid,
535 statp->n_gid, statp->n_muid);
536 break;
537 }
538 default:
539 break;
540 }
541 }
542 va_end(ap);
543
544 return offset - old_offset;
545}
546
547static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
548{
549 int8_t id = pdu->id + 1; /* Response */
550
551 if (len < 0) {
552 V9fsString str;
553 int err = -len;
554
555 str.data = strerror(err);
556 str.size = strlen(str.data);
557
558 len = 7;
559 len += pdu_marshal(pdu, len, "s", &str);
560 if (dotu) {
561 len += pdu_marshal(pdu, len, "d", err);
562 }
563
564 id = P9_RERROR;
565 }
566
567 /* fill out the header */
568 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
569
570 /* keep these in sync */
571 pdu->size = len;
572 pdu->id = id;
573
574 /* push onto queue and notify */
575 virtqueue_push(s->vq, &pdu->elem, len);
576
577 /* FIXME: we should batch these completions */
578 virtio_notify(&s->vdev, s->vq);
579
580 free_pdu(s, pdu);
581}
582
bb9e3216
AL
583static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
584{
585 mode_t ret;
586
587 ret = mode & 0777;
588 if (mode & P9_STAT_MODE_DIR) {
589 ret |= S_IFDIR;
590 }
591
592 if (dotu) {
593 if (mode & P9_STAT_MODE_SYMLINK) {
594 ret |= S_IFLNK;
595 }
596 if (mode & P9_STAT_MODE_SOCKET) {
597 ret |= S_IFSOCK;
598 }
599 if (mode & P9_STAT_MODE_NAMED_PIPE) {
600 ret |= S_IFIFO;
601 }
602 if (mode & P9_STAT_MODE_DEVICE) {
603 if (extension && extension->data[0] == 'c') {
604 ret |= S_IFCHR;
605 } else {
606 ret |= S_IFBLK;
607 }
608 }
609 }
610
611 if (!(ret&~0777)) {
612 ret |= S_IFREG;
613 }
614
615 if (mode & P9_STAT_MODE_SETUID) {
616 ret |= S_ISUID;
617 }
618 if (mode & P9_STAT_MODE_SETGID) {
619 ret |= S_ISGID;
620 }
621 if (mode & P9_STAT_MODE_SETVTX) {
622 ret |= S_ISVTX;
623 }
624
625 return ret;
626}
627
628static int donttouch_stat(V9fsStat *stat)
629{
630 if (stat->type == -1 &&
631 stat->dev == -1 &&
632 stat->qid.type == -1 &&
633 stat->qid.version == -1 &&
634 stat->qid.path == -1 &&
635 stat->mode == -1 &&
636 stat->atime == -1 &&
637 stat->mtime == -1 &&
638 stat->length == -1 &&
639 !stat->name.size &&
640 !stat->uid.size &&
641 !stat->gid.size &&
642 !stat->muid.size &&
643 stat->n_uid == -1 &&
644 stat->n_gid == -1 &&
645 stat->n_muid == -1) {
646 return 1;
647 }
648
649 return 0;
650}
651
652static void v9fs_stat_free(V9fsStat *stat)
653{
654 v9fs_string_free(&stat->name);
655 v9fs_string_free(&stat->uid);
656 v9fs_string_free(&stat->gid);
657 v9fs_string_free(&stat->muid);
658 v9fs_string_free(&stat->extension);
659}
660
661static uint32_t stat_to_v9mode(const struct stat *stbuf)
662{
663 uint32_t mode;
664
665 mode = stbuf->st_mode & 0777;
666 if (S_ISDIR(stbuf->st_mode)) {
667 mode |= P9_STAT_MODE_DIR;
668 }
669
670 if (dotu) {
671 if (S_ISLNK(stbuf->st_mode)) {
672 mode |= P9_STAT_MODE_SYMLINK;
673 }
674
675 if (S_ISSOCK(stbuf->st_mode)) {
676 mode |= P9_STAT_MODE_SOCKET;
677 }
678
679 if (S_ISFIFO(stbuf->st_mode)) {
680 mode |= P9_STAT_MODE_NAMED_PIPE;
681 }
682
683 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
684 mode |= P9_STAT_MODE_DEVICE;
685 }
686
687 if (stbuf->st_mode & S_ISUID) {
688 mode |= P9_STAT_MODE_SETUID;
689 }
690
691 if (stbuf->st_mode & S_ISGID) {
692 mode |= P9_STAT_MODE_SETGID;
693 }
694
695 if (stbuf->st_mode & S_ISVTX) {
696 mode |= P9_STAT_MODE_SETVTX;
697 }
698 }
699
700 return mode;
701}
702
703static int stat_to_v9stat(V9fsState *s, V9fsString *name,
704 const struct stat *stbuf,
705 V9fsStat *v9stat)
706{
707 int err;
708 const char *str;
709
710 memset(v9stat, 0, sizeof(*v9stat));
711
712 stat_to_qid(stbuf, &v9stat->qid);
713 v9stat->mode = stat_to_v9mode(stbuf);
714 v9stat->atime = stbuf->st_atime;
715 v9stat->mtime = stbuf->st_mtime;
716 v9stat->length = stbuf->st_size;
717
718 v9fs_string_null(&v9stat->uid);
719 v9fs_string_null(&v9stat->gid);
720 v9fs_string_null(&v9stat->muid);
721
722 if (dotu) {
723 v9stat->n_uid = stbuf->st_uid;
724 v9stat->n_gid = stbuf->st_gid;
725 v9stat->n_muid = 0;
726
727 v9fs_string_null(&v9stat->extension);
728
729 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
730 err = v9fs_do_readlink(s, name, &v9stat->extension);
731 if (err == -1) {
732 err = -errno;
733 return err;
734 }
735 v9stat->extension.data[err] = 0;
736 v9stat->extension.size = err;
737 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
738 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
739 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
740 major(stbuf->st_rdev), minor(stbuf->st_rdev));
741 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
742 v9fs_string_sprintf(&v9stat->extension, "%s %u",
743 "HARDLINKCOUNT", stbuf->st_nlink);
744 }
745 }
746
747 str = strrchr(name->data, '/');
748 if (str) {
749 str += 1;
750 } else {
751 str = name->data;
752 }
753
754 v9fs_string_sprintf(&v9stat->name, "%s", str);
755
756 v9stat->size = 61 +
757 v9fs_string_size(&v9stat->name) +
758 v9fs_string_size(&v9stat->uid) +
759 v9fs_string_size(&v9stat->gid) +
760 v9fs_string_size(&v9stat->muid) +
761 v9fs_string_size(&v9stat->extension);
762 return 0;
763}
764
1f5a89bf
AL
765static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
766{
767 while (len && *iovcnt) {
768 if (len < sg->iov_len) {
769 sg->iov_len -= len;
770 sg->iov_base += len;
771 len = 0;
772 } else {
773 len -= sg->iov_len;
774 sg++;
775 *iovcnt -= 1;
776 }
777 }
778
779 return sg;
780}
781
782static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
783{
784 int i;
785 int total = 0;
786
787 for (i = 0; i < *cnt; i++) {
788 if ((total + sg[i].iov_len) > cap) {
789 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
790 i++;
791 break;
792 }
793 total += sg[i].iov_len;
794 }
795
796 *cnt = i;
797
798 return sg;
799}
800
801static void print_sg(struct iovec *sg, int cnt)
802{
803 int i;
804
805 printf("sg[%d]: {", cnt);
806 for (i = 0; i < cnt; i++) {
807 if (i) {
808 printf(", ");
809 }
810 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
811 }
812 printf("}\n");
813}
814
405a549a
AL
815static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu)
816{
817 /* Note: The following have been added to prevent GCC from complaining
818 * They will be removed in the subsequent patches */
819 (void)pdu_unmarshal;
820 (void) complete_pdu;
a03f7874
AL
821 (void) v9fs_string_init;
822 (void) v9fs_string_free;
823 (void) v9fs_string_null;
824 (void) v9fs_string_sprintf;
825 (void) v9fs_string_copy;
826 (void) v9fs_string_size;
131dcb25
AL
827 (void) v9fs_do_lstat;
828 (void) v9fs_do_setuid;
829 (void) v9fs_do_readlink;
830 (void) v9fs_do_close;
831 (void) v9fs_do_closedir;
286d5652
AL
832 (void) alloc_fid;
833 (void) free_fid;
834 (void) fid_to_qid;
bb9e3216
AL
835 (void) v9mode_to_mode;
836 (void) donttouch_stat;
837 (void) v9fs_stat_free;
838 (void) stat_to_v9stat;
1f5a89bf
AL
839 (void) adjust_sg;
840 (void) cap_sg;
841 (void) print_sg;
405a549a 842}
131dcb25 843
9f107513
AL
844static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
845{
92c1ad03
AL
846 int32_t msize;
847 V9fsString version;
848 size_t offset = 7;
849
850 pdu_unmarshal(pdu, offset, "ds", &msize, &version);
851
852 if (strcmp(version.data, "9P2000.u")) {
853 v9fs_string_sprintf(&version, "unknown");
9f107513 854 }
92c1ad03
AL
855
856 offset += pdu_marshal(pdu, offset, "ds", msize, &version);
857 complete_pdu(s, pdu, offset);
858
859 v9fs_string_free(&version);
9f107513
AL
860}
861
862static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
863{
955efc47
AL
864 int32_t fid, afid, n_uname;
865 V9fsString uname, aname;
866 V9fsFidState *fidp;
867 V9fsQID qid;
868 size_t offset = 7;
869 ssize_t err;
870
871 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
872
873 fidp = alloc_fid(s, fid);
874 if (fidp == NULL) {
875 err = -EINVAL;
876 goto out;
9f107513 877 }
955efc47
AL
878
879 fidp->uid = n_uname;
880
881 v9fs_string_sprintf(&fidp->path, "%s", "/");
882 err = fid_to_qid(s, fidp, &qid);
883 if (err) {
884 err = -EINVAL;
885 free_fid(s, fid);
886 goto out;
887 }
888
889 offset += pdu_marshal(pdu, offset, "Q", &qid);
890
891 err = offset;
892out:
893 complete_pdu(s, pdu, err);
894 v9fs_string_free(&uname);
895 v9fs_string_free(&aname);
9f107513
AL
896}
897
4da7d3fa
AL
898typedef struct V9fsStatState {
899 V9fsPDU *pdu;
900 size_t offset;
901 V9fsStat v9stat;
902 V9fsFidState *fidp;
903 struct stat stbuf;
904} V9fsStatState;
905
906static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
907{
908 if (err == -1) {
909 err = -errno;
910 goto out;
911 }
912
913 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
914 if (err) {
915 goto out;
916 }
917 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
918 err = vs->offset;
919
920out:
921 complete_pdu(s, vs->pdu, err);
922 v9fs_stat_free(&vs->v9stat);
923 qemu_free(vs);
924}
925
9f107513
AL
926static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
927{
4da7d3fa
AL
928 int32_t fid;
929 V9fsStatState *vs;
930 ssize_t err = 0;
931
932 vs = qemu_malloc(sizeof(*vs));
933 vs->pdu = pdu;
934 vs->offset = 7;
935
936 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
937
938 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
939
940 vs->fidp = lookup_fid(s, fid);
941 if (vs->fidp == NULL) {
942 err = -ENOENT;
943 goto out;
9f107513 944 }
4da7d3fa
AL
945
946 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
947 v9fs_stat_post_lstat(s, vs, err);
948 return;
949
950out:
951 complete_pdu(s, vs->pdu, err);
952 v9fs_stat_free(&vs->v9stat);
953 qemu_free(vs);
9f107513
AL
954}
955
ff5e54c9
AL
956typedef struct V9fsWalkState {
957 V9fsPDU *pdu;
958 size_t offset;
959 int16_t nwnames;
960 int name_idx;
961 V9fsQID *qids;
962 V9fsFidState *fidp;
963 V9fsFidState *newfidp;
964 V9fsString path;
965 V9fsString *wnames;
966 struct stat stbuf;
967} V9fsWalkState;
968
969static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
970{
971 complete_pdu(s, vs->pdu, err);
972
973 if (vs->nwnames) {
974 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
975 v9fs_string_free(&vs->wnames[vs->name_idx]);
976 }
977
978 qemu_free(vs->wnames);
979 qemu_free(vs->qids);
980 }
981}
982
983static void v9fs_walk_marshal(V9fsWalkState *vs)
984{
985 int i;
986 vs->offset = 7;
987 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
988
989 for (i = 0; i < vs->nwnames; i++) {
990 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
991 }
992}
993
994static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
995 int err)
996{
997 if (err == -1) {
998 free_fid(s, vs->newfidp->fid);
999 v9fs_string_free(&vs->path);
1000 err = -ENOENT;
1001 goto out;
1002 }
1003
1004 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1005
1006 vs->name_idx++;
1007 if (vs->name_idx < vs->nwnames) {
1008 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1009 vs->wnames[vs->name_idx].data);
1010 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1011
1012 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1013 v9fs_walk_post_newfid_lstat(s, vs, err);
1014 return;
1015 }
1016
1017 v9fs_string_free(&vs->path);
1018 v9fs_walk_marshal(vs);
1019 err = vs->offset;
1020out:
1021 v9fs_walk_complete(s, vs, err);
1022}
1023
1024static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1025 int err)
1026{
1027 if (err == -1) {
1028 v9fs_string_free(&vs->path);
1029 err = -ENOENT;
1030 goto out;
1031 }
1032
1033 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1034 vs->name_idx++;
1035 if (vs->name_idx < vs->nwnames) {
1036
1037 v9fs_string_sprintf(&vs->path, "%s/%s",
1038 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1039 v9fs_string_copy(&vs->fidp->path, &vs->path);
1040
1041 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1042 v9fs_walk_post_oldfid_lstat(s, vs, err);
1043 return;
1044 }
1045
1046 v9fs_string_free(&vs->path);
1047 v9fs_walk_marshal(vs);
1048 err = vs->offset;
1049out:
1050 v9fs_walk_complete(s, vs, err);
1051}
1052
9f107513
AL
1053static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1054{
ff5e54c9
AL
1055 int32_t fid, newfid;
1056 V9fsWalkState *vs;
1057 int err = 0;
1058 int i;
1059
1060 vs = qemu_malloc(sizeof(*vs));
1061 vs->pdu = pdu;
1062 vs->wnames = NULL;
1063 vs->qids = NULL;
1064 vs->offset = 7;
1065
1066 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1067 &newfid, &vs->nwnames);
1068
1069 if (vs->nwnames) {
1070 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1071
1072 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1073
1074 for (i = 0; i < vs->nwnames; i++) {
1075 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1076 &vs->wnames[i]);
1077 }
1078 }
1079
1080 vs->fidp = lookup_fid(s, fid);
1081 if (vs->fidp == NULL) {
1082 err = -ENOENT;
1083 goto out;
1084 }
1085
1086 /* FIXME: is this really valid? */
1087 if (fid == newfid) {
1088
1089 BUG_ON(vs->fidp->fd != -1);
1090 BUG_ON(vs->fidp->dir);
1091 v9fs_string_init(&vs->path);
1092 vs->name_idx = 0;
1093
1094 if (vs->name_idx < vs->nwnames) {
1095 v9fs_string_sprintf(&vs->path, "%s/%s",
1096 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1097 v9fs_string_copy(&vs->fidp->path, &vs->path);
1098
1099 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1100 v9fs_walk_post_oldfid_lstat(s, vs, err);
1101 return;
1102 }
1103 } else {
1104 vs->newfidp = alloc_fid(s, newfid);
1105 if (vs->newfidp == NULL) {
1106 err = -EINVAL;
1107 goto out;
1108 }
1109
1110 vs->newfidp->uid = vs->fidp->uid;
1111 v9fs_string_init(&vs->path);
1112 vs->name_idx = 0;
1113 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1114
1115 if (vs->name_idx < vs->nwnames) {
1116 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1117 vs->wnames[vs->name_idx].data);
1118 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1119
1120 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1121 v9fs_walk_post_newfid_lstat(s, vs, err);
1122 return;
1123 }
9f107513 1124 }
ff5e54c9
AL
1125
1126 v9fs_walk_marshal(vs);
1127 err = vs->offset;
1128out:
1129 v9fs_walk_complete(s, vs, err);
9f107513
AL
1130}
1131
1132static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1133{
1134 if (debug_9p_pdu) {
1135 pprint_pdu(pdu);
1136 }
1137}
1138
1139static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1140{ if (debug_9p_pdu) {
1141 pprint_pdu(pdu);
1142 }
1143}
1144
1145static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1146{
1147 if (debug_9p_pdu) {
1148 pprint_pdu(pdu);
1149 }
1150}
1151
1152static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1153{
1154 if (debug_9p_pdu) {
1155 pprint_pdu(pdu);
1156 }
1157}
1158
1159static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1160{
1161 if (debug_9p_pdu) {
1162 pprint_pdu(pdu);
1163 }
1164}
1165
1166static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1167{
405a549a 1168 v9fs_dummy(s, pdu);
9f107513
AL
1169 if (debug_9p_pdu) {
1170 pprint_pdu(pdu);
1171 }
1172}
1173
1174static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1175{
1176 if (debug_9p_pdu) {
1177 pprint_pdu(pdu);
1178 }
1179}
1180
1181static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
1182{
1183 if (debug_9p_pdu) {
1184 pprint_pdu(pdu);
1185 }
1186}
1187
1188typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
1189
1190static pdu_handler_t *pdu_handlers[] = {
1191 [P9_TVERSION] = v9fs_version,
1192 [P9_TATTACH] = v9fs_attach,
1193 [P9_TSTAT] = v9fs_stat,
1194 [P9_TWALK] = v9fs_walk,
1195 [P9_TCLUNK] = v9fs_clunk,
1196 [P9_TOPEN] = v9fs_open,
1197 [P9_TREAD] = v9fs_read,
1198#if 0
1199 [P9_TAUTH] = v9fs_auth,
1200#endif
1201 [P9_TFLUSH] = v9fs_flush,
1202 [P9_TCREATE] = v9fs_create,
1203 [P9_TWRITE] = v9fs_write,
1204 [P9_TWSTAT] = v9fs_wstat,
1205 [P9_TREMOVE] = v9fs_remove,
1206};
1207
1208static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
1209{
1210 pdu_handler_t *handler;
1211
1212 if (debug_9p_pdu) {
1213 pprint_pdu(pdu);
1214 }
1215
1216 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
1217
1218 handler = pdu_handlers[pdu->id];
1219 BUG_ON(handler == NULL);
1220
1221 handler(s, pdu);
1222}
1223
1224static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
1225{
1226 V9fsState *s = (V9fsState *)vdev;
1227 V9fsPDU *pdu;
1228 ssize_t len;
1229
1230 while ((pdu = alloc_pdu(s)) &&
1231 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
1232 uint8_t *ptr;
1233
1234 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
1235 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
1236
1237 ptr = pdu->elem.out_sg[0].iov_base;
1238
1239 memcpy(&pdu->size, ptr, 4);
1240 pdu->id = ptr[4];
1241 memcpy(&pdu->tag, ptr + 5, 2);
1242
1243 submit_pdu(s, pdu);
1244 }
1245
1246 free_pdu(s, pdu);
1247}
1248
1249static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
1250{
1251 features |= 1 << VIRTIO_9P_MOUNT_TAG;
1252 return features;
1253}
1254
1255static V9fsState *to_virtio_9p(VirtIODevice *vdev)
1256{
1257 return (V9fsState *)vdev;
1258}
1259
1260static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
1261{
1262 struct virtio_9p_config *cfg;
1263 V9fsState *s = to_virtio_9p(vdev);
1264
1265 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
1266 s->tag_len);
1267 stw_raw(&cfg->tag_len, s->tag_len);
1268 memcpy(cfg->tag, s->tag, s->tag_len);
1269 memcpy(config, cfg, s->config_size);
1270 qemu_free(cfg);
1271}
1272
1273VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
1274 {
1275 V9fsState *s;
1276 int i, len;
1277 struct stat stat;
1278 FsTypeEntry *fse;
1279
1280
1281 s = (V9fsState *)virtio_common_init("virtio-9p",
1282 VIRTIO_ID_9P,
1283 sizeof(struct virtio_9p_config)+
1284 MAX_TAG_LEN,
1285 sizeof(V9fsState));
1286
1287 /* initialize pdu allocator */
1288 QLIST_INIT(&s->free_list);
1289 for (i = 0; i < (MAX_REQ - 1); i++) {
1290 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
1291 }
1292
1293 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
1294
1295 fse = get_fsdev_fsentry(conf->fsdev_id);
1296
1297 if (!fse) {
1298 /* We don't have a fsdev identified by fsdev_id */
1299 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
1300 "with the id %s\n", conf->fsdev_id);
1301 exit(1);
1302 }
1303
1304 if (!fse->path || !conf->tag) {
1305 /* we haven't specified a mount_tag or the path */
1306 fprintf(stderr, "fsdev with id %s needs path "
1307 "and Virtio-9p device needs mount_tag arguments\n",
1308 conf->fsdev_id);
1309 exit(1);
1310 }
1311
1312 if (lstat(fse->path, &stat)) {
1313 fprintf(stderr, "share path %s does not exist\n", fse->path);
1314 exit(1);
1315 } else if (!S_ISDIR(stat.st_mode)) {
1316 fprintf(stderr, "share path %s is not a directory \n", fse->path);
1317 exit(1);
1318 }
1319
1320 s->ctx.fs_root = qemu_strdup(fse->path);
1321 len = strlen(conf->tag);
1322 if (len > MAX_TAG_LEN) {
1323 len = MAX_TAG_LEN;
1324 }
1325 /* s->tag is non-NULL terminated string */
1326 s->tag = qemu_malloc(len);
1327 memcpy(s->tag, conf->tag, len);
1328 s->tag_len = len;
1329 s->ctx.uid = -1;
1330
1331 s->ops = fse->ops;
1332 s->vdev.get_features = virtio_9p_get_features;
1333 s->config_size = sizeof(struct virtio_9p_config) +
1334 s->tag_len;
1335 s->vdev.get_config = virtio_9p_get_config;
1336
1337 return &s->vdev;
1338}