]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-9p.c
virtio-9p: Security model for symlink and readlink
[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
fac4f111
VJ
24enum {
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,
34};
35
36static int omode_to_uflags(int8_t mode)
37{
38 int ret = 0;
39
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;
53 }
54
55 if (mode & Otrunc) {
56 ret |= O_TRUNC;
57 }
58
59 if (mode & Oappend) {
60 ret |= O_APPEND;
61 }
62
63 if (mode & Oexcl) {
64 ret |= O_EXCL;
65 }
66
67 return ret;
68}
69
758e8e38 70void cred_init(FsCred *credp)
131dcb25 71{
758e8e38
VJ
72 credp->fc_uid = -1;
73 credp->fc_gid = -1;
74 credp->fc_mode = -1;
75 credp->fc_rdev = -1;
131dcb25
AL
76}
77
758e8e38 78static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
131dcb25 79{
758e8e38 80 return s->ops->lstat(&s->ctx, path->data, stbuf);
131dcb25
AL
81}
82
83static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
84{
85 ssize_t len;
86
87 buf->data = qemu_malloc(1024);
88
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;
93 }
94
95 return len;
96}
97
98static int v9fs_do_close(V9fsState *s, int fd)
99{
100 return s->ops->close(&s->ctx, fd);
101}
102
103static int v9fs_do_closedir(V9fsState *s, DIR *dir)
104{
105 return s->ops->closedir(&s->ctx, dir);
106}
107
a6568fe2
AL
108static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
109{
110 return s->ops->open(&s->ctx, path->data, flags);
111}
112
113static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
114{
115 return s->ops->opendir(&s->ctx, path->data);
116}
117
a9231555
AL
118static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
119{
120 return s->ops->rewinddir(&s->ctx, dir);
121}
122
123static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
124{
125 return s->ops->telldir(&s->ctx, dir);
126}
127
128static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
129{
130 return s->ops->readdir(&s->ctx, dir);
131}
132
133static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
134{
135 return s->ops->seekdir(&s->ctx, dir, off);
136}
137
138static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
139 int iovcnt)
140{
141 return s->ops->readv(&s->ctx, fd, iov, iovcnt);
142}
143
144static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
145{
146 return s->ops->lseek(&s->ctx, fd, offset, whence);
147}
148
8449360c
AL
149static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
150 int iovcnt)
151{
152 return s->ops->writev(&s->ctx, fd, iov, iovcnt);
153}
154
c494dd6f
AL
155static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
156{
e95ead32
VJ
157 FsCred cred;
158 cred_init(&cred);
159 cred.fc_mode = mode;
160 return s->ops->chmod(&s->ctx, path->data, &cred);
c494dd6f
AL
161}
162
163static int v9fs_do_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev)
164{
165 return s->ops->mknod(&s->ctx, path->data, mode, dev);
166}
167
168static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
169{
170 return s->ops->mksock(&s->ctx, path->data);
171}
172
00ec5c37 173static int v9fs_do_mkdir(V9fsState *s, V9fsCreateState *vs)
c494dd6f 174{
00ec5c37
VJ
175 FsCred cred;
176
177 cred_init(&cred);
178 cred.fc_uid = vs->fidp->uid;
179 cred.fc_mode = vs->perm & 0777;
180
181 return s->ops->mkdir(&s->ctx, vs->fullname.data, &cred);
c494dd6f
AL
182}
183
184static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
185{
186 return s->ops->fstat(&s->ctx, fd, stbuf);
187}
188
4750a96f 189static int v9fs_do_open2(V9fsState *s, V9fsCreateState *vs)
c494dd6f 190{
4750a96f
VJ
191 FsCred cred;
192 int flags;
193
194 cred_init(&cred);
195 cred.fc_uid = vs->fidp->uid;
196 cred.fc_mode = vs->perm & 0777;
197 flags = omode_to_uflags(vs->mode) | O_CREAT;
198
199 return s->ops->open2(&s->ctx, vs->fullname.data, flags, &cred);
c494dd6f
AL
200}
201
879c2813 202static int v9fs_do_symlink(V9fsState *s, V9fsCreateState *vs)
c494dd6f 203{
879c2813
VJ
204 FsCred cred;
205 cred_init(&cred);
206 cred.fc_uid = vs->fidp->uid;
207 cred.fc_mode = vs->perm | 0777;
208
209 return s->ops->symlink(&s->ctx, vs->extension.data, vs->fullname.data,
210 &cred);
c494dd6f
AL
211}
212
213static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
214{
215 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
216}
217
8cf89e00
AL
218static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
219{
220 return s->ops->truncate(&s->ctx, path->data, size);
221}
222
223static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
224 V9fsString *newpath)
225{
226 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
227}
228
229static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
230{
f7613bee
VJ
231 FsCred cred;
232 cred_init(&cred);
233 cred.fc_uid = uid;
234 cred.fc_gid = gid;
235
236 return s->ops->chown(&s->ctx, path->data, &cred);
8cf89e00
AL
237}
238
239static int v9fs_do_utime(V9fsState *s, V9fsString *path,
240 const struct utimbuf *buf)
241{
242 return s->ops->utime(&s->ctx, path->data, buf);
243}
244
5bae1900
AL
245static int v9fs_do_remove(V9fsState *s, V9fsString *path)
246{
247 return s->ops->remove(&s->ctx, path->data);
248}
249
8cf89e00
AL
250static int v9fs_do_fsync(V9fsState *s, int fd)
251{
252 return s->ops->fsync(&s->ctx, fd);
253}
254
a03f7874
AL
255static void v9fs_string_init(V9fsString *str)
256{
257 str->data = NULL;
258 str->size = 0;
259}
260
261static void v9fs_string_free(V9fsString *str)
262{
263 qemu_free(str->data);
264 str->data = NULL;
265 str->size = 0;
266}
267
268static void v9fs_string_null(V9fsString *str)
269{
270 v9fs_string_free(str);
271}
272
273static int number_to_string(void *arg, char type)
274{
275 unsigned int ret = 0;
276
277 switch (type) {
278 case 'u': {
279 unsigned int num = *(unsigned int *)arg;
280
281 do {
282 ret++;
283 num = num/10;
284 } while (num);
285 break;
286 }
287 default:
288 printf("Number_to_string: Unknown number format\n");
289 return -1;
290 }
291
292 return ret;
293}
294
295static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
296{
297 va_list ap2;
298 char *iter = (char *)fmt;
299 int len = 0;
300 int nr_args = 0;
301 char *arg_char_ptr;
302 unsigned int arg_uint;
303
304 /* Find the number of %'s that denotes an argument */
305 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
306 nr_args++;
307 iter++;
308 }
309
310 len = strlen(fmt) - 2*nr_args;
311
312 if (!nr_args) {
313 goto alloc_print;
314 }
315
316 va_copy(ap2, ap);
317
318 iter = (char *)fmt;
319
320 /* Now parse the format string */
321 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
322 iter++;
323 switch (*iter) {
324 case 'u':
325 arg_uint = va_arg(ap2, unsigned int);
326 len += number_to_string((void *)&arg_uint, 'u');
327 break;
328 case 's':
329 arg_char_ptr = va_arg(ap2, char *);
330 len += strlen(arg_char_ptr);
331 break;
332 case 'c':
333 len += 1;
334 break;
335 default:
336 fprintf(stderr,
337 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
338 return -1;
339 }
340 iter++;
341 }
342
343alloc_print:
344 *strp = qemu_malloc((len + 1) * sizeof(**strp));
345
346 return vsprintf(*strp, fmt, ap);
347}
348
349static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
350{
351 va_list ap;
352 int err;
353
354 v9fs_string_free(str);
355
356 va_start(ap, fmt);
357 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
358 BUG_ON(err == -1);
359 va_end(ap);
360
361 str->size = err;
362}
363
364static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
365{
366 v9fs_string_free(lhs);
367 v9fs_string_sprintf(lhs, "%s", rhs->data);
368}
369
370static size_t v9fs_string_size(V9fsString *str)
371{
372 return str->size;
373}
374
286d5652
AL
375static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
376{
377 V9fsFidState *f;
378
379 for (f = s->fid_list; f; f = f->next) {
380 if (f->fid == fid) {
286d5652
AL
381 return f;
382 }
383 }
384
385 return NULL;
386}
387
388static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
389{
390 V9fsFidState *f;
391
392 f = lookup_fid(s, fid);
393 if (f) {
394 return NULL;
395 }
396
397 f = qemu_mallocz(sizeof(V9fsFidState));
398
399 f->fid = fid;
400 f->fd = -1;
401 f->dir = NULL;
402
403 f->next = s->fid_list;
404 s->fid_list = f;
405
406 return f;
407}
408
409static int free_fid(V9fsState *s, int32_t fid)
410{
411 V9fsFidState **fidpp, *fidp;
412
413 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
414 if ((*fidpp)->fid == fid) {
415 break;
416 }
417 }
418
419 if (*fidpp == NULL) {
420 return -ENOENT;
421 }
422
423 fidp = *fidpp;
424 *fidpp = fidp->next;
425
426 if (fidp->fd != -1) {
427 v9fs_do_close(s, fidp->fd);
428 }
429 if (fidp->dir) {
430 v9fs_do_closedir(s, fidp->dir);
431 }
432 v9fs_string_free(&fidp->path);
433 qemu_free(fidp);
434
435 return 0;
436}
437
438#define P9_QID_TYPE_DIR 0x80
439#define P9_QID_TYPE_SYMLINK 0x02
440
441#define P9_STAT_MODE_DIR 0x80000000
442#define P9_STAT_MODE_APPEND 0x40000000
443#define P9_STAT_MODE_EXCL 0x20000000
444#define P9_STAT_MODE_MOUNT 0x10000000
445#define P9_STAT_MODE_AUTH 0x08000000
446#define P9_STAT_MODE_TMP 0x04000000
447#define P9_STAT_MODE_SYMLINK 0x02000000
448#define P9_STAT_MODE_LINK 0x01000000
449#define P9_STAT_MODE_DEVICE 0x00800000
450#define P9_STAT_MODE_NAMED_PIPE 0x00200000
451#define P9_STAT_MODE_SOCKET 0x00100000
452#define P9_STAT_MODE_SETUID 0x00080000
453#define P9_STAT_MODE_SETGID 0x00040000
454#define P9_STAT_MODE_SETVTX 0x00010000
455
456#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
457 P9_STAT_MODE_SYMLINK | \
458 P9_STAT_MODE_LINK | \
459 P9_STAT_MODE_DEVICE | \
460 P9_STAT_MODE_NAMED_PIPE | \
461 P9_STAT_MODE_SOCKET)
462
463/* This is the algorithm from ufs in spfs */
464static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
465{
466 size_t size;
467
468 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
469 memcpy(&qidp->path, &stbuf->st_ino, size);
470 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
471 qidp->type = 0;
472 if (S_ISDIR(stbuf->st_mode)) {
473 qidp->type |= P9_QID_TYPE_DIR;
474 }
475 if (S_ISLNK(stbuf->st_mode)) {
476 qidp->type |= P9_QID_TYPE_SYMLINK;
477 }
478}
479
480static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
481{
482 struct stat stbuf;
483 int err;
484
485 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
486 if (err) {
487 return err;
488 }
489
490 stat_to_qid(&stbuf, qidp);
491 return 0;
492}
493
9f107513
AL
494static V9fsPDU *alloc_pdu(V9fsState *s)
495{
496 V9fsPDU *pdu = NULL;
497
498 if (!QLIST_EMPTY(&s->free_list)) {
499 pdu = QLIST_FIRST(&s->free_list);
500 QLIST_REMOVE(pdu, next);
501 }
502 return pdu;
503}
504
505static void free_pdu(V9fsState *s, V9fsPDU *pdu)
506{
507 if (pdu) {
508 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
509 }
510}
511
512size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
513 size_t offset, size_t size, int pack)
514{
515 int i = 0;
516 size_t copied = 0;
517
518 for (i = 0; size && i < sg_count; i++) {
519 size_t len;
520 if (offset >= sg[i].iov_len) {
521 /* skip this sg */
522 offset -= sg[i].iov_len;
523 continue;
524 } else {
525 len = MIN(sg[i].iov_len - offset, size);
526 if (pack) {
527 memcpy(sg[i].iov_base + offset, addr, len);
528 } else {
529 memcpy(addr, sg[i].iov_base + offset, len);
530 }
531 size -= len;
532 copied += len;
533 addr += len;
534 if (size) {
535 offset = 0;
536 continue;
537 }
538 }
539 }
540
541 return copied;
542}
543
405a549a
AL
544static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
545{
546 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
547 offset, size, 0);
548}
549
550static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
551 size_t size)
552{
553 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
554 offset, size, 1);
555}
556
557static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
558{
559 size_t pos = 0;
560 int i, j;
561 struct iovec *src_sg;
562 unsigned int num;
563
564 if (rx) {
565 src_sg = pdu->elem.in_sg;
566 num = pdu->elem.in_num;
567 } else {
568 src_sg = pdu->elem.out_sg;
569 num = pdu->elem.out_num;
570 }
571
572 j = 0;
573 for (i = 0; i < num; i++) {
574 if (offset <= pos) {
575 sg[j].iov_base = src_sg[i].iov_base;
576 sg[j].iov_len = src_sg[i].iov_len;
577 j++;
578 } else if (offset < (src_sg[i].iov_len + pos)) {
579 sg[j].iov_base = src_sg[i].iov_base;
580 sg[j].iov_len = src_sg[i].iov_len;
581 sg[j].iov_base += (offset - pos);
582 sg[j].iov_len -= (offset - pos);
583 j++;
584 }
585 pos += src_sg[i].iov_len;
586 }
587
588 return j;
589}
590
591static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
592{
593 size_t old_offset = offset;
594 va_list ap;
595 int i;
596
597 va_start(ap, fmt);
598 for (i = 0; fmt[i]; i++) {
599 switch (fmt[i]) {
600 case 'b': {
601 uint8_t *valp = va_arg(ap, uint8_t *);
602 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
603 break;
604 }
605 case 'w': {
606 uint16_t val, *valp;
607 valp = va_arg(ap, uint16_t *);
608 val = le16_to_cpupu(valp);
609 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
610 *valp = val;
611 break;
612 }
613 case 'd': {
614 uint32_t val, *valp;
615 valp = va_arg(ap, uint32_t *);
616 val = le32_to_cpupu(valp);
617 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
618 *valp = val;
619 break;
620 }
621 case 'q': {
622 uint64_t val, *valp;
623 valp = va_arg(ap, uint64_t *);
624 val = le64_to_cpup(valp);
625 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
626 *valp = val;
627 break;
628 }
629 case 'v': {
630 struct iovec *iov = va_arg(ap, struct iovec *);
631 int *iovcnt = va_arg(ap, int *);
632 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
633 break;
634 }
635 case 's': {
636 V9fsString *str = va_arg(ap, V9fsString *);
637 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
638 /* FIXME: sanity check str->size */
639 str->data = qemu_malloc(str->size + 1);
640 offset += pdu_unpack(str->data, pdu, offset, str->size);
641 str->data[str->size] = 0;
642 break;
643 }
644 case 'Q': {
645 V9fsQID *qidp = va_arg(ap, V9fsQID *);
646 offset += pdu_unmarshal(pdu, offset, "bdq",
647 &qidp->type, &qidp->version, &qidp->path);
648 break;
649 }
650 case 'S': {
651 V9fsStat *statp = va_arg(ap, V9fsStat *);
652 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
653 &statp->size, &statp->type, &statp->dev,
654 &statp->qid, &statp->mode, &statp->atime,
655 &statp->mtime, &statp->length,
656 &statp->name, &statp->uid, &statp->gid,
657 &statp->muid, &statp->extension,
658 &statp->n_uid, &statp->n_gid,
659 &statp->n_muid);
660 break;
661 }
662 default:
663 break;
664 }
665 }
666
667 va_end(ap);
668
669 return offset - old_offset;
670}
671
672static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
673{
674 size_t old_offset = offset;
675 va_list ap;
676 int i;
677
678 va_start(ap, fmt);
679 for (i = 0; fmt[i]; i++) {
680 switch (fmt[i]) {
681 case 'b': {
682 uint8_t val = va_arg(ap, int);
683 offset += pdu_pack(pdu, offset, &val, sizeof(val));
684 break;
685 }
686 case 'w': {
687 uint16_t val;
688 cpu_to_le16w(&val, va_arg(ap, int));
689 offset += pdu_pack(pdu, offset, &val, sizeof(val));
690 break;
691 }
692 case 'd': {
693 uint32_t val;
694 cpu_to_le32w(&val, va_arg(ap, uint32_t));
695 offset += pdu_pack(pdu, offset, &val, sizeof(val));
696 break;
697 }
698 case 'q': {
699 uint64_t val;
700 cpu_to_le64w(&val, va_arg(ap, uint64_t));
701 offset += pdu_pack(pdu, offset, &val, sizeof(val));
702 break;
703 }
704 case 'v': {
705 struct iovec *iov = va_arg(ap, struct iovec *);
706 int *iovcnt = va_arg(ap, int *);
707 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
708 break;
709 }
710 case 's': {
711 V9fsString *str = va_arg(ap, V9fsString *);
712 offset += pdu_marshal(pdu, offset, "w", str->size);
713 offset += pdu_pack(pdu, offset, str->data, str->size);
714 break;
715 }
716 case 'Q': {
717 V9fsQID *qidp = va_arg(ap, V9fsQID *);
718 offset += pdu_marshal(pdu, offset, "bdq",
719 qidp->type, qidp->version, qidp->path);
720 break;
721 }
722 case 'S': {
723 V9fsStat *statp = va_arg(ap, V9fsStat *);
724 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
725 statp->size, statp->type, statp->dev,
726 &statp->qid, statp->mode, statp->atime,
727 statp->mtime, statp->length, &statp->name,
728 &statp->uid, &statp->gid, &statp->muid,
729 &statp->extension, statp->n_uid,
730 statp->n_gid, statp->n_muid);
731 break;
732 }
733 default:
734 break;
735 }
736 }
737 va_end(ap);
738
739 return offset - old_offset;
740}
741
742static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
743{
744 int8_t id = pdu->id + 1; /* Response */
745
746 if (len < 0) {
747 V9fsString str;
748 int err = -len;
749
750 str.data = strerror(err);
751 str.size = strlen(str.data);
752
753 len = 7;
754 len += pdu_marshal(pdu, len, "s", &str);
755 if (dotu) {
756 len += pdu_marshal(pdu, len, "d", err);
757 }
758
759 id = P9_RERROR;
760 }
761
762 /* fill out the header */
763 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
764
765 /* keep these in sync */
766 pdu->size = len;
767 pdu->id = id;
768
769 /* push onto queue and notify */
770 virtqueue_push(s->vq, &pdu->elem, len);
771
772 /* FIXME: we should batch these completions */
773 virtio_notify(&s->vdev, s->vq);
774
775 free_pdu(s, pdu);
776}
777
bb9e3216
AL
778static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
779{
780 mode_t ret;
781
782 ret = mode & 0777;
783 if (mode & P9_STAT_MODE_DIR) {
784 ret |= S_IFDIR;
785 }
786
787 if (dotu) {
788 if (mode & P9_STAT_MODE_SYMLINK) {
789 ret |= S_IFLNK;
790 }
791 if (mode & P9_STAT_MODE_SOCKET) {
792 ret |= S_IFSOCK;
793 }
794 if (mode & P9_STAT_MODE_NAMED_PIPE) {
795 ret |= S_IFIFO;
796 }
797 if (mode & P9_STAT_MODE_DEVICE) {
798 if (extension && extension->data[0] == 'c') {
799 ret |= S_IFCHR;
800 } else {
801 ret |= S_IFBLK;
802 }
803 }
804 }
805
806 if (!(ret&~0777)) {
807 ret |= S_IFREG;
808 }
809
810 if (mode & P9_STAT_MODE_SETUID) {
811 ret |= S_ISUID;
812 }
813 if (mode & P9_STAT_MODE_SETGID) {
814 ret |= S_ISGID;
815 }
816 if (mode & P9_STAT_MODE_SETVTX) {
817 ret |= S_ISVTX;
818 }
819
820 return ret;
821}
822
823static int donttouch_stat(V9fsStat *stat)
824{
825 if (stat->type == -1 &&
826 stat->dev == -1 &&
827 stat->qid.type == -1 &&
828 stat->qid.version == -1 &&
829 stat->qid.path == -1 &&
830 stat->mode == -1 &&
831 stat->atime == -1 &&
832 stat->mtime == -1 &&
833 stat->length == -1 &&
834 !stat->name.size &&
835 !stat->uid.size &&
836 !stat->gid.size &&
837 !stat->muid.size &&
838 stat->n_uid == -1 &&
839 stat->n_gid == -1 &&
840 stat->n_muid == -1) {
841 return 1;
842 }
843
844 return 0;
845}
846
847static void v9fs_stat_free(V9fsStat *stat)
848{
849 v9fs_string_free(&stat->name);
850 v9fs_string_free(&stat->uid);
851 v9fs_string_free(&stat->gid);
852 v9fs_string_free(&stat->muid);
853 v9fs_string_free(&stat->extension);
854}
855
856static uint32_t stat_to_v9mode(const struct stat *stbuf)
857{
858 uint32_t mode;
859
860 mode = stbuf->st_mode & 0777;
861 if (S_ISDIR(stbuf->st_mode)) {
862 mode |= P9_STAT_MODE_DIR;
863 }
864
865 if (dotu) {
866 if (S_ISLNK(stbuf->st_mode)) {
867 mode |= P9_STAT_MODE_SYMLINK;
868 }
869
870 if (S_ISSOCK(stbuf->st_mode)) {
871 mode |= P9_STAT_MODE_SOCKET;
872 }
873
874 if (S_ISFIFO(stbuf->st_mode)) {
875 mode |= P9_STAT_MODE_NAMED_PIPE;
876 }
877
878 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
879 mode |= P9_STAT_MODE_DEVICE;
880 }
881
882 if (stbuf->st_mode & S_ISUID) {
883 mode |= P9_STAT_MODE_SETUID;
884 }
885
886 if (stbuf->st_mode & S_ISGID) {
887 mode |= P9_STAT_MODE_SETGID;
888 }
889
890 if (stbuf->st_mode & S_ISVTX) {
891 mode |= P9_STAT_MODE_SETVTX;
892 }
893 }
894
895 return mode;
896}
897
898static int stat_to_v9stat(V9fsState *s, V9fsString *name,
899 const struct stat *stbuf,
900 V9fsStat *v9stat)
901{
902 int err;
903 const char *str;
904
905 memset(v9stat, 0, sizeof(*v9stat));
906
907 stat_to_qid(stbuf, &v9stat->qid);
908 v9stat->mode = stat_to_v9mode(stbuf);
909 v9stat->atime = stbuf->st_atime;
910 v9stat->mtime = stbuf->st_mtime;
911 v9stat->length = stbuf->st_size;
912
913 v9fs_string_null(&v9stat->uid);
914 v9fs_string_null(&v9stat->gid);
915 v9fs_string_null(&v9stat->muid);
916
917 if (dotu) {
918 v9stat->n_uid = stbuf->st_uid;
919 v9stat->n_gid = stbuf->st_gid;
920 v9stat->n_muid = 0;
921
922 v9fs_string_null(&v9stat->extension);
923
924 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
925 err = v9fs_do_readlink(s, name, &v9stat->extension);
926 if (err == -1) {
927 err = -errno;
928 return err;
929 }
930 v9stat->extension.data[err] = 0;
931 v9stat->extension.size = err;
932 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
933 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
934 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
935 major(stbuf->st_rdev), minor(stbuf->st_rdev));
936 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
937 v9fs_string_sprintf(&v9stat->extension, "%s %u",
938 "HARDLINKCOUNT", stbuf->st_nlink);
939 }
940 }
941
942 str = strrchr(name->data, '/');
943 if (str) {
944 str += 1;
945 } else {
946 str = name->data;
947 }
948
949 v9fs_string_sprintf(&v9stat->name, "%s", str);
950
951 v9stat->size = 61 +
952 v9fs_string_size(&v9stat->name) +
953 v9fs_string_size(&v9stat->uid) +
954 v9fs_string_size(&v9stat->gid) +
955 v9fs_string_size(&v9stat->muid) +
956 v9fs_string_size(&v9stat->extension);
957 return 0;
958}
959
1f5a89bf
AL
960static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
961{
962 while (len && *iovcnt) {
963 if (len < sg->iov_len) {
964 sg->iov_len -= len;
965 sg->iov_base += len;
966 len = 0;
967 } else {
968 len -= sg->iov_len;
969 sg++;
970 *iovcnt -= 1;
971 }
972 }
973
974 return sg;
975}
976
977static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
978{
979 int i;
980 int total = 0;
981
982 for (i = 0; i < *cnt; i++) {
983 if ((total + sg[i].iov_len) > cap) {
984 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
985 i++;
986 break;
987 }
988 total += sg[i].iov_len;
989 }
990
991 *cnt = i;
992
993 return sg;
994}
995
996static void print_sg(struct iovec *sg, int cnt)
997{
998 int i;
999
1000 printf("sg[%d]: {", cnt);
1001 for (i = 0; i < cnt; i++) {
1002 if (i) {
1003 printf(", ");
1004 }
1005 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1006 }
1007 printf("}\n");
1008}
1009
8cf89e00
AL
1010static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1011{
1012 V9fsString str;
1013 v9fs_string_init(&str);
1014 v9fs_string_copy(&str, dst);
1015 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1016 v9fs_string_free(&str);
1017}
1018
9f107513
AL
1019static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1020{
92c1ad03
AL
1021 int32_t msize;
1022 V9fsString version;
1023 size_t offset = 7;
1024
1025 pdu_unmarshal(pdu, offset, "ds", &msize, &version);
1026
1027 if (strcmp(version.data, "9P2000.u")) {
1028 v9fs_string_sprintf(&version, "unknown");
9f107513 1029 }
92c1ad03
AL
1030
1031 offset += pdu_marshal(pdu, offset, "ds", msize, &version);
1032 complete_pdu(s, pdu, offset);
1033
1034 v9fs_string_free(&version);
9f107513
AL
1035}
1036
1037static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1038{
955efc47
AL
1039 int32_t fid, afid, n_uname;
1040 V9fsString uname, aname;
1041 V9fsFidState *fidp;
1042 V9fsQID qid;
1043 size_t offset = 7;
1044 ssize_t err;
1045
1046 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1047
1048 fidp = alloc_fid(s, fid);
1049 if (fidp == NULL) {
1050 err = -EINVAL;
1051 goto out;
9f107513 1052 }
955efc47
AL
1053
1054 fidp->uid = n_uname;
1055
1056 v9fs_string_sprintf(&fidp->path, "%s", "/");
1057 err = fid_to_qid(s, fidp, &qid);
1058 if (err) {
1059 err = -EINVAL;
1060 free_fid(s, fid);
1061 goto out;
1062 }
1063
1064 offset += pdu_marshal(pdu, offset, "Q", &qid);
1065
1066 err = offset;
1067out:
1068 complete_pdu(s, pdu, err);
1069 v9fs_string_free(&uname);
1070 v9fs_string_free(&aname);
9f107513
AL
1071}
1072
4da7d3fa
AL
1073static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1074{
1075 if (err == -1) {
1076 err = -errno;
1077 goto out;
1078 }
1079
1080 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1081 if (err) {
1082 goto out;
1083 }
1084 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1085 err = vs->offset;
1086
1087out:
1088 complete_pdu(s, vs->pdu, err);
1089 v9fs_stat_free(&vs->v9stat);
1090 qemu_free(vs);
1091}
1092
9f107513
AL
1093static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1094{
4da7d3fa
AL
1095 int32_t fid;
1096 V9fsStatState *vs;
1097 ssize_t err = 0;
1098
1099 vs = qemu_malloc(sizeof(*vs));
1100 vs->pdu = pdu;
1101 vs->offset = 7;
1102
1103 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1104
1105 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1106
1107 vs->fidp = lookup_fid(s, fid);
1108 if (vs->fidp == NULL) {
1109 err = -ENOENT;
1110 goto out;
9f107513 1111 }
4da7d3fa
AL
1112
1113 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1114 v9fs_stat_post_lstat(s, vs, err);
1115 return;
1116
1117out:
1118 complete_pdu(s, vs->pdu, err);
1119 v9fs_stat_free(&vs->v9stat);
1120 qemu_free(vs);
9f107513
AL
1121}
1122
ff5e54c9
AL
1123static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1124{
1125 complete_pdu(s, vs->pdu, err);
1126
1127 if (vs->nwnames) {
1128 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1129 v9fs_string_free(&vs->wnames[vs->name_idx]);
1130 }
1131
1132 qemu_free(vs->wnames);
1133 qemu_free(vs->qids);
1134 }
1135}
1136
1137static void v9fs_walk_marshal(V9fsWalkState *vs)
1138{
1139 int i;
1140 vs->offset = 7;
1141 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1142
1143 for (i = 0; i < vs->nwnames; i++) {
1144 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1145 }
1146}
1147
1148static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1149 int err)
1150{
1151 if (err == -1) {
1152 free_fid(s, vs->newfidp->fid);
1153 v9fs_string_free(&vs->path);
1154 err = -ENOENT;
1155 goto out;
1156 }
1157
1158 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1159
1160 vs->name_idx++;
1161 if (vs->name_idx < vs->nwnames) {
1162 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1163 vs->wnames[vs->name_idx].data);
1164 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1165
1166 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1167 v9fs_walk_post_newfid_lstat(s, vs, err);
1168 return;
1169 }
1170
1171 v9fs_string_free(&vs->path);
1172 v9fs_walk_marshal(vs);
1173 err = vs->offset;
1174out:
1175 v9fs_walk_complete(s, vs, err);
1176}
1177
1178static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1179 int err)
1180{
1181 if (err == -1) {
1182 v9fs_string_free(&vs->path);
1183 err = -ENOENT;
1184 goto out;
1185 }
1186
1187 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1188 vs->name_idx++;
1189 if (vs->name_idx < vs->nwnames) {
1190
1191 v9fs_string_sprintf(&vs->path, "%s/%s",
1192 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1193 v9fs_string_copy(&vs->fidp->path, &vs->path);
1194
1195 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1196 v9fs_walk_post_oldfid_lstat(s, vs, err);
1197 return;
1198 }
1199
1200 v9fs_string_free(&vs->path);
1201 v9fs_walk_marshal(vs);
1202 err = vs->offset;
1203out:
1204 v9fs_walk_complete(s, vs, err);
1205}
1206
9f107513
AL
1207static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1208{
ff5e54c9
AL
1209 int32_t fid, newfid;
1210 V9fsWalkState *vs;
1211 int err = 0;
1212 int i;
1213
1214 vs = qemu_malloc(sizeof(*vs));
1215 vs->pdu = pdu;
1216 vs->wnames = NULL;
1217 vs->qids = NULL;
1218 vs->offset = 7;
1219
1220 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1221 &newfid, &vs->nwnames);
1222
1223 if (vs->nwnames) {
1224 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1225
1226 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1227
1228 for (i = 0; i < vs->nwnames; i++) {
1229 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1230 &vs->wnames[i]);
1231 }
1232 }
1233
1234 vs->fidp = lookup_fid(s, fid);
1235 if (vs->fidp == NULL) {
1236 err = -ENOENT;
1237 goto out;
1238 }
1239
1240 /* FIXME: is this really valid? */
1241 if (fid == newfid) {
1242
1243 BUG_ON(vs->fidp->fd != -1);
1244 BUG_ON(vs->fidp->dir);
1245 v9fs_string_init(&vs->path);
1246 vs->name_idx = 0;
1247
1248 if (vs->name_idx < vs->nwnames) {
1249 v9fs_string_sprintf(&vs->path, "%s/%s",
1250 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1251 v9fs_string_copy(&vs->fidp->path, &vs->path);
1252
1253 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1254 v9fs_walk_post_oldfid_lstat(s, vs, err);
1255 return;
1256 }
1257 } else {
1258 vs->newfidp = alloc_fid(s, newfid);
1259 if (vs->newfidp == NULL) {
1260 err = -EINVAL;
1261 goto out;
1262 }
1263
1264 vs->newfidp->uid = vs->fidp->uid;
1265 v9fs_string_init(&vs->path);
1266 vs->name_idx = 0;
1267 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1268
1269 if (vs->name_idx < vs->nwnames) {
1270 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1271 vs->wnames[vs->name_idx].data);
1272 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1273
1274 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1275 v9fs_walk_post_newfid_lstat(s, vs, err);
1276 return;
1277 }
9f107513 1278 }
ff5e54c9
AL
1279
1280 v9fs_walk_marshal(vs);
1281 err = vs->offset;
1282out:
1283 v9fs_walk_complete(s, vs, err);
9f107513
AL
1284}
1285
a6568fe2
AL
1286static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1287{
1288 if (vs->fidp->dir == NULL) {
1289 err = -errno;
1290 goto out;
1291 }
1292
1293 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1294 err = vs->offset;
1295out:
1296 complete_pdu(s, vs->pdu, err);
1297 qemu_free(vs);
1298
1299}
1300
1301static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1302{
1303 if (vs->fidp->fd == -1) {
1304 err = -errno;
1305 goto out;
1306 }
1307
1308 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1309 err = vs->offset;
1310out:
1311 complete_pdu(s, vs->pdu, err);
1312 qemu_free(vs);
1313}
1314
1315static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1316{
1317 if (err) {
1318 err = -errno;
1319 goto out;
1320 }
1321
1322 stat_to_qid(&vs->stbuf, &vs->qid);
1323
1324 if (S_ISDIR(vs->stbuf.st_mode)) {
1325 vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1326 v9fs_open_post_opendir(s, vs, err);
1327 } else {
1328 vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1329 omode_to_uflags(vs->mode));
1330 v9fs_open_post_open(s, vs, err);
1331 }
1332 return;
1333out:
1334 complete_pdu(s, vs->pdu, err);
1335 qemu_free(vs);
9f107513
AL
1336}
1337
1338static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
a6568fe2
AL
1339{
1340 int32_t fid;
1341 V9fsOpenState *vs;
1342 ssize_t err = 0;
1343
1344
1345 vs = qemu_malloc(sizeof(*vs));
1346 vs->pdu = pdu;
1347 vs->offset = 7;
1348
1349 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1350
1351 vs->fidp = lookup_fid(s, fid);
1352 if (vs->fidp == NULL) {
1353 err = -ENOENT;
1354 goto out;
1355 }
1356
1357 BUG_ON(vs->fidp->fd != -1);
1358 BUG_ON(vs->fidp->dir);
1359
1360 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1361
1362 v9fs_open_post_lstat(s, vs, err);
1363 return;
1364out:
1365 complete_pdu(s, pdu, err);
1366 qemu_free(vs);
1367}
1368
1369static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1370{
bbd5697b
AL
1371 int32_t fid;
1372 size_t offset = 7;
1373 int err;
1374
1375 pdu_unmarshal(pdu, offset, "d", &fid);
1376
1377 err = free_fid(s, fid);
1378 if (err < 0) {
1379 goto out;
a6568fe2 1380 }
bbd5697b
AL
1381
1382 offset = 7;
1383 err = offset;
1384out:
1385 complete_pdu(s, pdu, err);
9f107513
AL
1386}
1387
a9231555
AL
1388static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1389
1390static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1391{
1392 if (err) {
1393 goto out;
1394 }
1395 v9fs_stat_free(&vs->v9stat);
1396 v9fs_string_free(&vs->name);
1397 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1398 vs->offset += vs->count;
1399 err = vs->offset;
1400out:
1401 complete_pdu(s, vs->pdu, err);
1402 qemu_free(vs);
1403 return;
1404}
1405
1406static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1407 ssize_t err)
1408{
1409 if (err) {
1410 err = -errno;
1411 goto out;
1412 }
1413 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1414 if (err) {
1415 goto out;
1416 }
1417
1418 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1419 &vs->v9stat);
1420 if ((vs->len != (vs->v9stat.size + 2)) ||
1421 ((vs->count + vs->len) > vs->max_count)) {
1422 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1423 v9fs_read_post_seekdir(s, vs, err);
1424 return;
1425 }
1426 vs->count += vs->len;
1427 v9fs_stat_free(&vs->v9stat);
1428 v9fs_string_free(&vs->name);
1429 vs->dir_pos = vs->dent->d_off;
1430 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1431 v9fs_read_post_readdir(s, vs, err);
1432 return;
1433out:
1434 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1435 v9fs_read_post_seekdir(s, vs, err);
1436 return;
1437
1438}
1439
1440static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1441{
1442 if (vs->dent) {
1443 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1444 v9fs_string_init(&vs->name);
1445 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1446 vs->dent->d_name);
1447 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1448 v9fs_read_post_dir_lstat(s, vs, err);
1449 return;
1450 }
1451
1452 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1453 vs->offset += vs->count;
1454 err = vs->offset;
1455 complete_pdu(s, vs->pdu, err);
1456 qemu_free(vs);
1457 return;
1458}
1459
1460static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1461{
1462 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1463 v9fs_read_post_readdir(s, vs, err);
1464 return;
1465}
1466
1467static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1468 ssize_t err)
1469{
1470 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1471 v9fs_read_post_telldir(s, vs, err);
1472 return;
1473}
1474
1475static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1476{
1477 if (err < 0) {
1478 /* IO error return the error */
1479 err = -errno;
1480 goto out;
1481 }
1482 vs->total += vs->len;
1483 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1484 if (vs->total < vs->count && vs->len > 0) {
1485 do {
1486 if (0) {
1487 print_sg(vs->sg, vs->cnt);
1488 }
1489 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1490 } while (vs->len == -1 && errno == EINTR);
1491 if (vs->len == -1) {
1492 err = -errno;
1493 }
1494 v9fs_read_post_readv(s, vs, err);
1495 return;
1496 }
1497 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1498 vs->offset += vs->count;
1499 err = vs->offset;
1500
1501out:
1502 complete_pdu(s, vs->pdu, err);
1503 qemu_free(vs);
1504}
1505
1506static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1507{
1508 if (err == -1) {
1509 err = -errno;
1510 goto out;
1511 }
1512 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1513
1514 if (vs->total < vs->count) {
1515 do {
1516 if (0) {
1517 print_sg(vs->sg, vs->cnt);
1518 }
1519 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1520 } while (vs->len == -1 && errno == EINTR);
1521 if (vs->len == -1) {
1522 err = -errno;
1523 }
1524 v9fs_read_post_readv(s, vs, err);
1525 return;
1526 }
1527out:
1528 complete_pdu(s, vs->pdu, err);
1529 qemu_free(vs);
1530}
1531
9f107513
AL
1532static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1533{
a9231555
AL
1534 int32_t fid;
1535 V9fsReadState *vs;
1536 ssize_t err = 0;
1537
1538 vs = qemu_malloc(sizeof(*vs));
1539 vs->pdu = pdu;
1540 vs->offset = 7;
1541 vs->total = 0;
1542 vs->len = 0;
1543 vs->count = 0;
1544
1545 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1546
1547 vs->fidp = lookup_fid(s, fid);
1548 if (vs->fidp == NULL) {
1549 err = -EINVAL;
1550 goto out;
1551 }
1552
1553 if (vs->fidp->dir) {
1554 vs->max_count = vs->count;
1555 vs->count = 0;
1556 if (vs->off == 0) {
1557 v9fs_do_rewinddir(s, vs->fidp->dir);
1558 }
1559 v9fs_read_post_rewinddir(s, vs, err);
1560 return;
1561 } else if (vs->fidp->fd != -1) {
1562 vs->sg = vs->iov;
1563 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1564 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1565 v9fs_read_post_lseek(s, vs, err);
1566 return;
1567 } else {
1568 err = -EINVAL;
9f107513 1569 }
a9231555
AL
1570out:
1571 complete_pdu(s, pdu, err);
1572 qemu_free(vs);
9f107513
AL
1573}
1574
8449360c
AL
1575static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1576 ssize_t err)
1577{
1578 if (err < 0) {
1579 /* IO error return the error */
1580 err = -errno;
1581 goto out;
1582 }
1583 vs->total += vs->len;
1584 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1585 if (vs->total < vs->count && vs->len > 0) {
1586 do {
1587 if (0) {
1588 print_sg(vs->sg, vs->cnt);
1589 }
1590 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1591 } while (vs->len == -1 && errno == EINTR);
1592 if (vs->len == -1) {
1593 err = -errno;
1594 }
1595 v9fs_write_post_writev(s, vs, err);
1596 return;
1597 }
1598 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1599
1600 err = vs->offset;
1601out:
1602 complete_pdu(s, vs->pdu, err);
1603 qemu_free(vs);
1604}
1605
1606static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1607{
1608 if (err == -1) {
1609 err = -errno;
1610 goto out;
1611 }
1612 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1613
1614 if (vs->total < vs->count) {
1615 do {
1616 if (0) {
1617 print_sg(vs->sg, vs->cnt);
1618 }
1619 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1620 } while (vs->len == -1 && errno == EINTR);
1621 if (vs->len == -1) {
1622 err = -errno;
1623 }
1624 v9fs_write_post_writev(s, vs, err);
1625 return;
1626 }
1627
1628out:
1629 complete_pdu(s, vs->pdu, err);
1630 qemu_free(vs);
1631}
1632
9f107513
AL
1633static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1634{
8449360c
AL
1635 int32_t fid;
1636 V9fsWriteState *vs;
1637 ssize_t err;
1638
1639 vs = qemu_malloc(sizeof(*vs));
1640
1641 vs->pdu = pdu;
1642 vs->offset = 7;
1643 vs->sg = vs->iov;
1644 vs->total = 0;
1645 vs->len = 0;
1646
1647 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1648 vs->sg, &vs->cnt);
1649
1650 vs->fidp = lookup_fid(s, fid);
1651 if (vs->fidp == NULL) {
1652 err = -EINVAL;
1653 goto out;
9f107513 1654 }
8449360c
AL
1655
1656 if (vs->fidp->fd == -1) {
1657 err = -EINVAL;
1658 goto out;
1659 }
1660
1661 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1662
1663 v9fs_write_post_lseek(s, vs, err);
1664 return;
1665
1666out:
1667 complete_pdu(s, vs->pdu, err);
1668 qemu_free(vs);
9f107513
AL
1669}
1670
c494dd6f
AL
1671static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1672{
1673 if (err == 0) {
1674 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1675 stat_to_qid(&vs->stbuf, &vs->qid);
1676
1677 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1678
1679 err = vs->offset;
1680 }
1681
1682 complete_pdu(s, vs->pdu, err);
1683 v9fs_string_free(&vs->name);
1684 v9fs_string_free(&vs->extension);
1685 v9fs_string_free(&vs->fullname);
1686 qemu_free(vs);
1687}
1688
1689static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1690{
1691 if (err) {
1692 err = -errno;
1693 }
1694 v9fs_post_create(s, vs, err);
1695}
1696
1697static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1698 int err)
1699{
1700 if (!vs->fidp->dir) {
1701 err = -errno;
1702 }
1703 v9fs_post_create(s, vs, err);
1704}
1705
1706static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1707 int err)
1708{
1709 if (err) {
1710 err = -errno;
1711 goto out;
1712 }
1713
1714 vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1715 v9fs_create_post_opendir(s, vs, err);
1716 return;
1717
1718out:
1719 v9fs_post_create(s, vs, err);
1720}
1721
1722static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1723{
1724 if (err) {
1725 err = -errno;
1726 goto out;
1727 }
1728
1729 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1730 v9fs_create_post_dir_lstat(s, vs, err);
1731 return;
1732
1733out:
1734 v9fs_post_create(s, vs, err);
1735}
1736
1737static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1738 int err)
1739{
1740 if (err) {
1741 err = -errno;
1742 goto out;
1743 }
1744
1745 err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1746 v9fs_create_post_perms(s, vs, err);
1747 return;
1748
1749out:
1750 v9fs_post_create(s, vs, err);
1751}
1752
1753static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1754{
1755 if (err) {
1756 vs->fidp->fd = -1;
1757 err = -errno;
1758 }
1759
1760 v9fs_post_create(s, vs, err);
1761 return;
1762}
1763
1764static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1765{
1766 if (vs->fidp->fd == -1) {
1767 err = -errno;
1768 goto out;
1769 }
1770
1771 err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1772 v9fs_create_post_fstat(s, vs, err);
1773
1774 return;
1775
1776out:
1777 v9fs_post_create(s, vs, err);
1778
1779}
1780
1781static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1782{
1783
1784 if (err == 0 || errno != ENOENT) {
1785 err = -errno;
1786 goto out;
1787 }
1788
1789 if (vs->perm & P9_STAT_MODE_DIR) {
00ec5c37 1790 err = v9fs_do_mkdir(s, vs);
c494dd6f
AL
1791 v9fs_create_post_mkdir(s, vs, err);
1792 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
879c2813 1793 err = v9fs_do_symlink(s, vs);
c494dd6f
AL
1794 v9fs_create_post_perms(s, vs, err);
1795 } else if (vs->perm & P9_STAT_MODE_LINK) {
1796 int32_t nfid = atoi(vs->extension.data);
1797 V9fsFidState *nfidp = lookup_fid(s, nfid);
1798 if (nfidp == NULL) {
1799 err = -errno;
1800 v9fs_post_create(s, vs, err);
1801 }
1802 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1803 v9fs_create_post_perms(s, vs, err);
1804 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1805 char ctype;
1806 uint32_t major, minor;
1807 mode_t nmode = 0;
1808
1809 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1810 &minor) != 3) {
1811 err = -errno;
1812 v9fs_post_create(s, vs, err);
1813 }
1814
1815 switch (ctype) {
1816 case 'c':
1817 nmode = S_IFCHR;
1818 break;
1819 case 'b':
1820 nmode = S_IFBLK;
1821 break;
1822 default:
1823 err = -EIO;
1824 v9fs_post_create(s, vs, err);
1825 }
1826
1827 nmode |= vs->perm & 0777;
1828 err = v9fs_do_mknod(s, &vs->fullname, nmode, makedev(major, minor));
1829 v9fs_create_post_perms(s, vs, err);
1830 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1831 err = v9fs_do_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
1832 v9fs_post_create(s, vs, err);
1833 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1834 err = v9fs_do_mksock(s, &vs->fullname);
1835 v9fs_create_post_mksock(s, vs, err);
1836 } else {
4750a96f 1837 vs->fidp->fd = v9fs_do_open2(s, vs);
c494dd6f
AL
1838 v9fs_create_post_open2(s, vs, err);
1839 }
1840
1841 return;
1842
1843out:
1844 v9fs_post_create(s, vs, err);
1845}
1846
9f107513
AL
1847static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1848{
c494dd6f
AL
1849 int32_t fid;
1850 V9fsCreateState *vs;
1851 int err = 0;
1852
1853 vs = qemu_malloc(sizeof(*vs));
1854 vs->pdu = pdu;
1855 vs->offset = 7;
1856
1857 v9fs_string_init(&vs->fullname);
1858
1859 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1860 &vs->perm, &vs->mode, &vs->extension);
1861
1862 vs->fidp = lookup_fid(s, fid);
1863 if (vs->fidp == NULL) {
1864 err = -EINVAL;
1865 goto out;
9f107513 1866 }
c494dd6f
AL
1867
1868 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1869 vs->name.data);
1870
1871 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1872 v9fs_create_post_lstat(s, vs, err);
1873 return;
1874
1875out:
1876 complete_pdu(s, vs->pdu, err);
1877 v9fs_string_free(&vs->name);
1878 v9fs_string_free(&vs->extension);
1879 qemu_free(vs);
9f107513
AL
1880}
1881
1882static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1883{
9c5e9d89
AL
1884 /* A nop call with no return */
1885 complete_pdu(s, pdu, 7);
9f107513
AL
1886}
1887
5bae1900
AL
1888static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1889 int err)
1890{
1891 /* For TREMOVE we need to clunk the fid even on failed remove */
1892 err = free_fid(s, vs->fidp->fid);
1893 if (err < 0) {
1894 goto out;
1895 }
1896
1897 err = vs->offset;
1898out:
1899 complete_pdu(s, vs->pdu, err);
1900 qemu_free(vs);
1901}
1902
9f107513
AL
1903static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1904{
5bae1900
AL
1905 int32_t fid;
1906 V9fsRemoveState *vs;
1907 int err = 0;
1908
1909 vs = qemu_malloc(sizeof(*vs));
1910 vs->pdu = pdu;
1911 vs->offset = 7;
1912
1913 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1914
1915 vs->fidp = lookup_fid(s, fid);
1916 if (vs->fidp == NULL) {
1917 err = -EINVAL;
1918 goto out;
9f107513 1919 }
5bae1900
AL
1920
1921 err = v9fs_do_remove(s, &vs->fidp->path);
1922 v9fs_remove_post_remove(s, vs, err);
1923 return;
1924
1925out:
1926 complete_pdu(s, pdu, err);
1927 qemu_free(vs);
9f107513
AL
1928}
1929
8cf89e00
AL
1930static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1931{
1932 if (err < 0) {
1933 goto out;
1934 }
1935
1936 err = vs->offset;
1937
1938out:
1939 v9fs_stat_free(&vs->v9stat);
1940 complete_pdu(s, vs->pdu, err);
1941 qemu_free(vs);
1942}
1943
1944static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
1945{
1946 if (err < 0) {
1947 goto out;
1948 }
1949
1950 if (vs->v9stat.name.size != 0) {
1951 v9fs_string_free(&vs->nname);
1952 }
1953
1954 if (vs->v9stat.length != -1) {
1955 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
1956 err = -errno;
1957 }
1958 }
1959 v9fs_wstat_post_truncate(s, vs, err);
1960 return;
1961
1962out:
1963 v9fs_stat_free(&vs->v9stat);
1964 complete_pdu(s, vs->pdu, err);
1965 qemu_free(vs);
1966}
1967
1968static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
1969{
1970 V9fsFidState *fidp;
1971 if (err < 0) {
1972 goto out;
1973 }
1974
1975 if (vs->v9stat.name.size != 0) {
1976 char *old_name, *new_name;
1977 char *end;
1978
1979 old_name = vs->fidp->path.data;
1980 end = strrchr(old_name, '/');
1981 if (end) {
1982 end++;
1983 } else {
1984 end = old_name;
1985 }
1986
1987 new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
1988
1989 memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
1990 memcpy(new_name, old_name, end - old_name);
1991 memcpy(new_name + (end - old_name), vs->v9stat.name.data,
1992 vs->v9stat.name.size);
1993 vs->nname.data = new_name;
1994 vs->nname.size = strlen(new_name);
1995
1996 if (strcmp(new_name, vs->fidp->path.data) != 0) {
1997 if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
1998 err = -errno;
1999 } else {
2000 /*
2001 * Fixup fid's pointing to the old name to
2002 * start pointing to the new name
2003 */
2004 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2005
2006 if (vs->fidp == fidp) {
2007 /*
2008 * we replace name of this fid towards the end
2009 * so that our below strcmp will work
2010 */
2011 continue;
2012 }
2013 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2014 strlen(vs->fidp->path.data))) {
2015 /* replace the name */
2016 v9fs_fix_path(&fidp->path, &vs->nname,
2017 strlen(vs->fidp->path.data));
2018 }
2019 }
2020 v9fs_string_copy(&vs->fidp->path, &vs->nname);
2021 }
2022 }
2023 }
2024 v9fs_wstat_post_rename(s, vs, err);
2025 return;
2026
2027out:
2028 v9fs_stat_free(&vs->v9stat);
2029 complete_pdu(s, vs->pdu, err);
2030 qemu_free(vs);
2031}
2032
2033static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2034{
2035 if (err < 0) {
2036 goto out;
2037 }
2038
f7613bee 2039 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
8cf89e00
AL
2040 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2041 vs->v9stat.n_gid)) {
2042 err = -errno;
2043 }
2044 }
2045 v9fs_wstat_post_chown(s, vs, err);
2046 return;
2047
2048out:
2049 v9fs_stat_free(&vs->v9stat);
2050 complete_pdu(s, vs->pdu, err);
2051 qemu_free(vs);
2052}
2053
2054static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2055{
2056 if (err < 0) {
2057 goto out;
2058 }
2059
2060 if (vs->v9stat.mtime != -1) {
2061 struct utimbuf tb;
2062 tb.actime = 0;
2063 tb.modtime = vs->v9stat.mtime;
2064 if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2065 err = -errno;
2066 }
2067 }
2068
2069 v9fs_wstat_post_utime(s, vs, err);
2070 return;
2071
2072out:
2073 v9fs_stat_free(&vs->v9stat);
2074 complete_pdu(s, vs->pdu, err);
2075 qemu_free(vs);
2076}
2077
2078static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2079{
2080 if (err == -1) {
2081 err = -errno;
2082 }
2083 v9fs_stat_free(&vs->v9stat);
2084 complete_pdu(s, vs->pdu, err);
2085 qemu_free(vs);
2086}
2087
2088static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2089{
2090 uint32_t v9_mode;
2091
2092 if (err == -1) {
2093 err = -errno;
2094 goto out;
2095 }
2096
2097 v9_mode = stat_to_v9mode(&vs->stbuf);
2098
2099 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2100 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2101 /* Attempting to change the type */
2102 err = -EIO;
2103 goto out;
2104 }
2105
2106 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2107 &vs->v9stat.extension))) {
2108 err = -errno;
2109 }
2110 v9fs_wstat_post_chmod(s, vs, err);
2111 return;
2112
2113out:
2114 v9fs_stat_free(&vs->v9stat);
2115 complete_pdu(s, vs->pdu, err);
2116 qemu_free(vs);
2117}
2118
9f107513
AL
2119static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2120{
8cf89e00
AL
2121 int32_t fid;
2122 V9fsWstatState *vs;
2123 int err = 0;
2124
2125 vs = qemu_malloc(sizeof(*vs));
2126 vs->pdu = pdu;
2127 vs->offset = 7;
2128
2129 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2130
2131 vs->fidp = lookup_fid(s, fid);
2132 if (vs->fidp == NULL) {
2133 err = -EINVAL;
2134 goto out;
9f107513 2135 }
8cf89e00
AL
2136
2137 /* do we need to sync the file? */
2138 if (donttouch_stat(&vs->v9stat)) {
2139 err = v9fs_do_fsync(s, vs->fidp->fd);
2140 v9fs_wstat_post_fsync(s, vs, err);
2141 return;
2142 }
2143
2144 if (vs->v9stat.mode != -1) {
2145 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2146 v9fs_wstat_post_lstat(s, vs, err);
2147 return;
2148 }
2149
2150 v9fs_wstat_post_chmod(s, vs, err);
2151 return;
2152
2153out:
2154 v9fs_stat_free(&vs->v9stat);
2155 complete_pdu(s, vs->pdu, err);
2156 qemu_free(vs);
9f107513
AL
2157}
2158
2159typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2160
2161static pdu_handler_t *pdu_handlers[] = {
2162 [P9_TVERSION] = v9fs_version,
2163 [P9_TATTACH] = v9fs_attach,
2164 [P9_TSTAT] = v9fs_stat,
2165 [P9_TWALK] = v9fs_walk,
2166 [P9_TCLUNK] = v9fs_clunk,
2167 [P9_TOPEN] = v9fs_open,
2168 [P9_TREAD] = v9fs_read,
2169#if 0
2170 [P9_TAUTH] = v9fs_auth,
2171#endif
2172 [P9_TFLUSH] = v9fs_flush,
2173 [P9_TCREATE] = v9fs_create,
2174 [P9_TWRITE] = v9fs_write,
2175 [P9_TWSTAT] = v9fs_wstat,
2176 [P9_TREMOVE] = v9fs_remove,
2177};
2178
2179static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2180{
2181 pdu_handler_t *handler;
2182
2183 if (debug_9p_pdu) {
2184 pprint_pdu(pdu);
2185 }
2186
2187 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2188
2189 handler = pdu_handlers[pdu->id];
2190 BUG_ON(handler == NULL);
2191
2192 handler(s, pdu);
2193}
2194
2195static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2196{
2197 V9fsState *s = (V9fsState *)vdev;
2198 V9fsPDU *pdu;
2199 ssize_t len;
2200
2201 while ((pdu = alloc_pdu(s)) &&
2202 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2203 uint8_t *ptr;
2204
2205 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2206 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2207
2208 ptr = pdu->elem.out_sg[0].iov_base;
2209
2210 memcpy(&pdu->size, ptr, 4);
2211 pdu->id = ptr[4];
2212 memcpy(&pdu->tag, ptr + 5, 2);
2213
2214 submit_pdu(s, pdu);
2215 }
2216
2217 free_pdu(s, pdu);
2218}
2219
2220static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2221{
2222 features |= 1 << VIRTIO_9P_MOUNT_TAG;
2223 return features;
2224}
2225
2226static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2227{
2228 return (V9fsState *)vdev;
2229}
2230
2231static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2232{
2233 struct virtio_9p_config *cfg;
2234 V9fsState *s = to_virtio_9p(vdev);
2235
2236 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2237 s->tag_len);
2238 stw_raw(&cfg->tag_len, s->tag_len);
2239 memcpy(cfg->tag, s->tag, s->tag_len);
2240 memcpy(config, cfg, s->config_size);
2241 qemu_free(cfg);
2242}
2243
2244VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2245 {
2246 V9fsState *s;
2247 int i, len;
2248 struct stat stat;
2249 FsTypeEntry *fse;
2250
2251
2252 s = (V9fsState *)virtio_common_init("virtio-9p",
2253 VIRTIO_ID_9P,
2254 sizeof(struct virtio_9p_config)+
2255 MAX_TAG_LEN,
2256 sizeof(V9fsState));
2257
2258 /* initialize pdu allocator */
2259 QLIST_INIT(&s->free_list);
2260 for (i = 0; i < (MAX_REQ - 1); i++) {
2261 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2262 }
2263
2264 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2265
2266 fse = get_fsdev_fsentry(conf->fsdev_id);
2267
2268 if (!fse) {
2269 /* We don't have a fsdev identified by fsdev_id */
2270 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2271 "with the id %s\n", conf->fsdev_id);
2272 exit(1);
2273 }
2274
2275 if (!fse->path || !conf->tag) {
2276 /* we haven't specified a mount_tag or the path */
2277 fprintf(stderr, "fsdev with id %s needs path "
2278 "and Virtio-9p device needs mount_tag arguments\n",
2279 conf->fsdev_id);
2280 exit(1);
2281 }
2282
758e8e38
VJ
2283 if (!strcmp(fse->security_model, "passthrough")) {
2284 /* Files on the Fileserver set to client user credentials */
2285 s->ctx.fs_sm = SM_PASSTHROUGH;
2286 } else if (!strcmp(fse->security_model, "mapped")) {
2287 /* Files on the fileserver are set to QEMU credentials.
2288 * Client user credentials are saved in extended attributes.
2289 */
2290 s->ctx.fs_sm = SM_MAPPED;
2291 } else {
9ce56db6
VJ
2292 /* user haven't specified a correct security option */
2293 fprintf(stderr, "one of the following must be specified as the"
2294 "security option:\n\t security_model=passthrough \n\t "
2295 "security_model=mapped\n");
2296 return NULL;
2297 }
2298
9f107513
AL
2299 if (lstat(fse->path, &stat)) {
2300 fprintf(stderr, "share path %s does not exist\n", fse->path);
2301 exit(1);
2302 } else if (!S_ISDIR(stat.st_mode)) {
2303 fprintf(stderr, "share path %s is not a directory \n", fse->path);
2304 exit(1);
2305 }
2306
2307 s->ctx.fs_root = qemu_strdup(fse->path);
2308 len = strlen(conf->tag);
2309 if (len > MAX_TAG_LEN) {
2310 len = MAX_TAG_LEN;
2311 }
2312 /* s->tag is non-NULL terminated string */
2313 s->tag = qemu_malloc(len);
2314 memcpy(s->tag, conf->tag, len);
2315 s->tag_len = len;
2316 s->ctx.uid = -1;
2317
2318 s->ops = fse->ops;
2319 s->vdev.get_features = virtio_9p_get_features;
2320 s->config_size = sizeof(struct virtio_9p_config) +
2321 s->tag_len;
2322 s->vdev.get_config = virtio_9p_get_config;
2323
2324 return &s->vdev;
2325}