]> Git Repo - qemu.git/blob - hw/virtio-9p.c
virtio-9p: Add fid and qid management support.
[qemu.git] / hw / virtio-9p.c
1 /*
2  * Virtio 9p backend
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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
21 int dotu = 1;
22 int debug_9p_pdu;
23
24 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
25 {
26     return s->ops->lstat(&s->ctx, path->data, stbuf);
27 }
28
29 static int v9fs_do_setuid(V9fsState *s, uid_t uid)
30 {
31     return s->ops->setuid(&s->ctx, uid);
32 }
33
34 static 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
49 static int v9fs_do_close(V9fsState *s, int fd)
50 {
51     return s->ops->close(&s->ctx, fd);
52 }
53
54 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
55 {
56     return s->ops->closedir(&s->ctx, dir);
57 }
58
59 static void v9fs_string_init(V9fsString *str)
60 {
61     str->data = NULL;
62     str->size = 0;
63 }
64
65 static void v9fs_string_free(V9fsString *str)
66 {
67     qemu_free(str->data);
68     str->data = NULL;
69     str->size = 0;
70 }
71
72 static void v9fs_string_null(V9fsString *str)
73 {
74     v9fs_string_free(str);
75 }
76
77 static 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
99 static 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
147 alloc_print:
148     *strp = qemu_malloc((len + 1) * sizeof(**strp));
149
150     return vsprintf(*strp, fmt, ap);
151 }
152
153 static 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
168 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
169 {
170     v9fs_string_free(lhs);
171     v9fs_string_sprintf(lhs, "%s", rhs->data);
172 }
173
174 static size_t v9fs_string_size(V9fsString *str)
175 {
176     return str->size;
177 }
178
179 static 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
193 static 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
214 static 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 */
269 static 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
285 static 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
299 static 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
310 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
311 {
312     if (pdu) {
313         QLIST_INSERT_HEAD(&s->free_list, pdu, next);
314     }
315 }
316
317 size_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
349 static 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
355 static 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
362 static 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
396 static 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
477 static 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
547 static 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
583 static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu)
584 {
585     /* Note: The following have been added to prevent GCC from complaining
586      * They will be removed in the subsequent patches */
587     (void)pdu_unmarshal;
588     (void) complete_pdu;
589     (void) v9fs_string_init;
590     (void) v9fs_string_free;
591     (void) v9fs_string_null;
592     (void) v9fs_string_sprintf;
593     (void) v9fs_string_copy;
594     (void) v9fs_string_size;
595     (void) v9fs_do_lstat;
596     (void) v9fs_do_setuid;
597     (void) v9fs_do_readlink;
598     (void) v9fs_do_close;
599     (void) v9fs_do_closedir;
600     (void) alloc_fid;
601     (void) free_fid;
602     (void) fid_to_qid;
603 }
604
605 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
606 {
607     if (debug_9p_pdu) {
608         pprint_pdu(pdu);
609     }
610 }
611
612 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
613 {
614     if (debug_9p_pdu) {
615         pprint_pdu(pdu);
616     }
617 }
618
619 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
620 {
621     if (debug_9p_pdu) {
622         pprint_pdu(pdu);
623     }
624 }
625
626 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
627 {
628     if (debug_9p_pdu) {
629         pprint_pdu(pdu);
630     }
631 }
632
633 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
634 {
635     if (debug_9p_pdu) {
636         pprint_pdu(pdu);
637     }
638 }
639
640 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
641 {    if (debug_9p_pdu) {
642         pprint_pdu(pdu);
643      }
644 }
645
646 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
647 {
648     if (debug_9p_pdu) {
649         pprint_pdu(pdu);
650     }
651 }
652
653 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
654 {
655     if (debug_9p_pdu) {
656         pprint_pdu(pdu);
657     }
658 }
659
660 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
661 {
662     if (debug_9p_pdu) {
663         pprint_pdu(pdu);
664     }
665 }
666
667 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
668 {
669     v9fs_dummy(s, pdu);
670     if (debug_9p_pdu) {
671         pprint_pdu(pdu);
672     }
673 }
674
675 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
676 {
677     if (debug_9p_pdu) {
678         pprint_pdu(pdu);
679     }
680 }
681
682 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
683 {
684     if (debug_9p_pdu) {
685         pprint_pdu(pdu);
686     }
687 }
688
689 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
690
691 static pdu_handler_t *pdu_handlers[] = {
692     [P9_TVERSION] = v9fs_version,
693     [P9_TATTACH] = v9fs_attach,
694     [P9_TSTAT] = v9fs_stat,
695     [P9_TWALK] = v9fs_walk,
696     [P9_TCLUNK] = v9fs_clunk,
697     [P9_TOPEN] = v9fs_open,
698     [P9_TREAD] = v9fs_read,
699 #if 0
700     [P9_TAUTH] = v9fs_auth,
701 #endif
702     [P9_TFLUSH] = v9fs_flush,
703     [P9_TCREATE] = v9fs_create,
704     [P9_TWRITE] = v9fs_write,
705     [P9_TWSTAT] = v9fs_wstat,
706     [P9_TREMOVE] = v9fs_remove,
707 };
708
709 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
710 {
711     pdu_handler_t *handler;
712
713     if (debug_9p_pdu) {
714         pprint_pdu(pdu);
715     }
716
717     BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
718
719     handler = pdu_handlers[pdu->id];
720     BUG_ON(handler == NULL);
721
722     handler(s, pdu);
723 }
724
725 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
726 {
727     V9fsState *s = (V9fsState *)vdev;
728     V9fsPDU *pdu;
729     ssize_t len;
730
731     while ((pdu = alloc_pdu(s)) &&
732             (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
733         uint8_t *ptr;
734
735         BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
736         BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
737
738         ptr = pdu->elem.out_sg[0].iov_base;
739
740         memcpy(&pdu->size, ptr, 4);
741         pdu->id = ptr[4];
742         memcpy(&pdu->tag, ptr + 5, 2);
743
744         submit_pdu(s, pdu);
745     }
746
747     free_pdu(s, pdu);
748 }
749
750 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
751 {
752     features |= 1 << VIRTIO_9P_MOUNT_TAG;
753     return features;
754 }
755
756 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
757 {
758     return (V9fsState *)vdev;
759 }
760
761 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
762 {
763     struct virtio_9p_config *cfg;
764     V9fsState *s = to_virtio_9p(vdev);
765
766     cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
767                         s->tag_len);
768     stw_raw(&cfg->tag_len, s->tag_len);
769     memcpy(cfg->tag, s->tag, s->tag_len);
770     memcpy(config, cfg, s->config_size);
771     qemu_free(cfg);
772 }
773
774 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
775  {
776     V9fsState *s;
777     int i, len;
778     struct stat stat;
779     FsTypeEntry *fse;
780
781
782     s = (V9fsState *)virtio_common_init("virtio-9p",
783                                     VIRTIO_ID_9P,
784                                     sizeof(struct virtio_9p_config)+
785                                     MAX_TAG_LEN,
786                                     sizeof(V9fsState));
787
788     /* initialize pdu allocator */
789     QLIST_INIT(&s->free_list);
790     for (i = 0; i < (MAX_REQ - 1); i++) {
791         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
792     }
793
794     s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
795
796     fse = get_fsdev_fsentry(conf->fsdev_id);
797
798     if (!fse) {
799         /* We don't have a fsdev identified by fsdev_id */
800         fprintf(stderr, "Virtio-9p device couldn't find fsdev "
801                     "with the id %s\n", conf->fsdev_id);
802         exit(1);
803     }
804
805     if (!fse->path || !conf->tag) {
806         /* we haven't specified a mount_tag or the path */
807         fprintf(stderr, "fsdev with id %s needs path "
808                 "and Virtio-9p device needs mount_tag arguments\n",
809                 conf->fsdev_id);
810         exit(1);
811     }
812
813     if (lstat(fse->path, &stat)) {
814         fprintf(stderr, "share path %s does not exist\n", fse->path);
815         exit(1);
816     } else if (!S_ISDIR(stat.st_mode)) {
817         fprintf(stderr, "share path %s is not a directory \n", fse->path);
818         exit(1);
819     }
820
821     s->ctx.fs_root = qemu_strdup(fse->path);
822     len = strlen(conf->tag);
823     if (len > MAX_TAG_LEN) {
824         len = MAX_TAG_LEN;
825     }
826     /* s->tag is non-NULL terminated string */
827     s->tag = qemu_malloc(len);
828     memcpy(s->tag, conf->tag, len);
829     s->tag_len = len;
830     s->ctx.uid = -1;
831
832     s->ops = fse->ops;
833     s->vdev.get_features = virtio_9p_get_features;
834     s->config_size = sizeof(struct virtio_9p_config) +
835                         s->tag_len;
836     s->vdev.get_config = virtio_9p_get_config;
837
838     return &s->vdev;
839 }
This page took 0.070706 seconds and 4 git commands to generate.