Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ f7613bee

History | View | Annotate | Download (55.5 kB)

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

    
21
int dotu = 1;
22
int debug_9p_pdu;
23

    
24
enum {
25
    Oread   = 0x00,
26
    Owrite  = 0x01,
27
    Ordwr   = 0x02,
28
    Oexec   = 0x03,
29
    Oexcl   = 0x04,
30
    Otrunc  = 0x10,
31
    Orexec  = 0x20,
32
    Orclose = 0x40,
33
    Oappend = 0x80,
34
};
35

    
36
static 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

    
70
void cred_init(FsCred *credp)
71
{
72
    credp->fc_uid = -1;
73
    credp->fc_gid = -1;
74
    credp->fc_mode = -1;
75
    credp->fc_rdev = -1;
76
}
77

    
78
static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
79
{
80
    return s->ops->lstat(&s->ctx, path->data, stbuf);
81
}
82

    
83
static 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

    
98
static int v9fs_do_close(V9fsState *s, int fd)
99
{
100
    return s->ops->close(&s->ctx, fd);
101
}
102

    
103
static int v9fs_do_closedir(V9fsState *s, DIR *dir)
104
{
105
    return s->ops->closedir(&s->ctx, dir);
106
}
107

    
108
static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
109
{
110
    return s->ops->open(&s->ctx, path->data, flags);
111
}
112

    
113
static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
114
{
115
    return s->ops->opendir(&s->ctx, path->data);
116
}
117

    
118
static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
119
{
120
    return s->ops->rewinddir(&s->ctx, dir);
121
}
122

    
123
static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
124
{
125
    return s->ops->telldir(&s->ctx, dir);
126
}
127

    
128
static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
129
{
130
    return s->ops->readdir(&s->ctx, dir);
131
}
132

    
133
static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
134
{
135
    return s->ops->seekdir(&s->ctx, dir, off);
136
}
137

    
138
static 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

    
144
static 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

    
149
static 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

    
155
static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
156
{
157
    FsCred cred;
158
    cred_init(&cred);
159
    cred.fc_mode = mode;
160
    return s->ops->chmod(&s->ctx, path->data, &cred);
161
}
162

    
163
static 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

    
168
static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
169
{
170
    return s->ops->mksock(&s->ctx, path->data);
171
}
172

    
173
static int v9fs_do_mkdir(V9fsState *s, V9fsString *path, mode_t mode)
174
{
175
    return s->ops->mkdir(&s->ctx, path->data, mode);
176
}
177

    
178
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
179
{
180
    return s->ops->fstat(&s->ctx, fd, stbuf);
181
}
182

    
183
static int v9fs_do_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode)
184
{
185
    return s->ops->open2(&s->ctx, path->data, flags, mode);
186
}
187

    
188
static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath,
189
                            V9fsString *newpath)
190
{
191
    return s->ops->symlink(&s->ctx, oldpath->data, newpath->data);
192
}
193

    
194
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
195
{
196
    return s->ops->link(&s->ctx, oldpath->data, newpath->data);
197
}
198

    
199
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
200
{
201
    return s->ops->truncate(&s->ctx, path->data, size);
202
}
203

    
204
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
205
                            V9fsString *newpath)
206
{
207
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
208
}
209

    
210
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
211
{
212
    FsCred cred;
213
    cred_init(&cred);
214
    cred.fc_uid = uid;
215
    cred.fc_gid = gid;
216

    
217
    return s->ops->chown(&s->ctx, path->data, &cred);
218
}
219

    
220
static int v9fs_do_utime(V9fsState *s, V9fsString *path,
221
                            const struct utimbuf *buf)
222
{
223
    return s->ops->utime(&s->ctx, path->data, buf);
224
}
225

    
226
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
227
{
228
    return s->ops->remove(&s->ctx, path->data);
229
}
230

    
231
static int v9fs_do_fsync(V9fsState *s, int fd)
232
{
233
    return s->ops->fsync(&s->ctx, fd);
234
}
235

    
236
static void v9fs_string_init(V9fsString *str)
237
{
238
    str->data = NULL;
239
    str->size = 0;
240
}
241

    
242
static void v9fs_string_free(V9fsString *str)
243
{
244
    qemu_free(str->data);
245
    str->data = NULL;
246
    str->size = 0;
247
}
248

    
249
static void v9fs_string_null(V9fsString *str)
250
{
251
    v9fs_string_free(str);
252
}
253

    
254
static int number_to_string(void *arg, char type)
255
{
256
    unsigned int ret = 0;
257

    
258
    switch (type) {
259
    case 'u': {
260
        unsigned int num = *(unsigned int *)arg;
261

    
262
        do {
263
            ret++;
264
            num = num/10;
265
        } while (num);
266
        break;
267
    }
268
    default:
269
        printf("Number_to_string: Unknown number format\n");
270
        return -1;
271
    }
272

    
273
    return ret;
274
}
275

    
276
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
277
{
278
    va_list ap2;
279
    char *iter = (char *)fmt;
280
    int len = 0;
281
    int nr_args = 0;
282
    char *arg_char_ptr;
283
    unsigned int arg_uint;
284

    
285
    /* Find the number of %'s that denotes an argument */
286
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
287
        nr_args++;
288
        iter++;
289
    }
290

    
291
    len = strlen(fmt) - 2*nr_args;
292

    
293
    if (!nr_args) {
294
        goto alloc_print;
295
    }
296

    
297
    va_copy(ap2, ap);
298

    
299
    iter = (char *)fmt;
300

    
301
    /* Now parse the format string */
302
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
303
        iter++;
304
        switch (*iter) {
305
        case 'u':
306
            arg_uint = va_arg(ap2, unsigned int);
307
            len += number_to_string((void *)&arg_uint, 'u');
308
            break;
309
        case 's':
310
            arg_char_ptr = va_arg(ap2, char *);
311
            len += strlen(arg_char_ptr);
312
            break;
313
        case 'c':
314
            len += 1;
315
            break;
316
        default:
317
            fprintf(stderr,
318
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
319
            return -1;
320
        }
321
        iter++;
322
    }
323

    
324
alloc_print:
325
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
326

    
327
    return vsprintf(*strp, fmt, ap);
328
}
329

    
330
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
331
{
332
    va_list ap;
333
    int err;
334

    
335
    v9fs_string_free(str);
336

    
337
    va_start(ap, fmt);
338
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
339
    BUG_ON(err == -1);
340
    va_end(ap);
341

    
342
    str->size = err;
343
}
344

    
345
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
346
{
347
    v9fs_string_free(lhs);
348
    v9fs_string_sprintf(lhs, "%s", rhs->data);
349
}
350

    
351
static size_t v9fs_string_size(V9fsString *str)
352
{
353
    return str->size;
354
}
355

    
356
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
357
{
358
    V9fsFidState *f;
359

    
360
    for (f = s->fid_list; f; f = f->next) {
361
        if (f->fid == fid) {
362
            return f;
363
        }
364
    }
365

    
366
    return NULL;
367
}
368

    
369
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
370
{
371
    V9fsFidState *f;
372

    
373
    f = lookup_fid(s, fid);
374
    if (f) {
375
        return NULL;
376
    }
377

    
378
    f = qemu_mallocz(sizeof(V9fsFidState));
379

    
380
    f->fid = fid;
381
    f->fd = -1;
382
    f->dir = NULL;
383

    
384
    f->next = s->fid_list;
385
    s->fid_list = f;
386

    
387
    return f;
388
}
389

    
390
static int free_fid(V9fsState *s, int32_t fid)
391
{
392
    V9fsFidState **fidpp, *fidp;
393

    
394
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
395
        if ((*fidpp)->fid == fid) {
396
            break;
397
        }
398
    }
399

    
400
    if (*fidpp == NULL) {
401
        return -ENOENT;
402
    }
403

    
404
    fidp = *fidpp;
405
    *fidpp = fidp->next;
406

    
407
    if (fidp->fd != -1) {
408
        v9fs_do_close(s, fidp->fd);
409
    }
410
    if (fidp->dir) {
411
        v9fs_do_closedir(s, fidp->dir);
412
    }
413
    v9fs_string_free(&fidp->path);
414
    qemu_free(fidp);
415

    
416
    return 0;
417
}
418

    
419
#define P9_QID_TYPE_DIR         0x80
420
#define P9_QID_TYPE_SYMLINK     0x02
421

    
422
#define P9_STAT_MODE_DIR        0x80000000
423
#define P9_STAT_MODE_APPEND     0x40000000
424
#define P9_STAT_MODE_EXCL       0x20000000
425
#define P9_STAT_MODE_MOUNT      0x10000000
426
#define P9_STAT_MODE_AUTH       0x08000000
427
#define P9_STAT_MODE_TMP        0x04000000
428
#define P9_STAT_MODE_SYMLINK    0x02000000
429
#define P9_STAT_MODE_LINK       0x01000000
430
#define P9_STAT_MODE_DEVICE     0x00800000
431
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
432
#define P9_STAT_MODE_SOCKET     0x00100000
433
#define P9_STAT_MODE_SETUID     0x00080000
434
#define P9_STAT_MODE_SETGID     0x00040000
435
#define P9_STAT_MODE_SETVTX     0x00010000
436

    
437
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
438
                                P9_STAT_MODE_SYMLINK |      \
439
                                P9_STAT_MODE_LINK |         \
440
                                P9_STAT_MODE_DEVICE |       \
441
                                P9_STAT_MODE_NAMED_PIPE |   \
442
                                P9_STAT_MODE_SOCKET)
443

    
444
/* This is the algorithm from ufs in spfs */
445
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
446
{
447
    size_t size;
448

    
449
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
450
    memcpy(&qidp->path, &stbuf->st_ino, size);
451
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
452
    qidp->type = 0;
453
    if (S_ISDIR(stbuf->st_mode)) {
454
        qidp->type |= P9_QID_TYPE_DIR;
455
    }
456
    if (S_ISLNK(stbuf->st_mode)) {
457
        qidp->type |= P9_QID_TYPE_SYMLINK;
458
    }
459
}
460

    
461
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
462
{
463
    struct stat stbuf;
464
    int err;
465

    
466
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
467
    if (err) {
468
        return err;
469
    }
470

    
471
    stat_to_qid(&stbuf, qidp);
472
    return 0;
473
}
474

    
475
static V9fsPDU *alloc_pdu(V9fsState *s)
476
{
477
    V9fsPDU *pdu = NULL;
478

    
479
    if (!QLIST_EMPTY(&s->free_list)) {
480
        pdu = QLIST_FIRST(&s->free_list);
481
        QLIST_REMOVE(pdu, next);
482
    }
483
    return pdu;
484
}
485

    
486
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
487
{
488
    if (pdu) {
489
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
490
    }
491
}
492

    
493
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
494
                        size_t offset, size_t size, int pack)
495
{
496
    int i = 0;
497
    size_t copied = 0;
498

    
499
    for (i = 0; size && i < sg_count; i++) {
500
        size_t len;
501
        if (offset >= sg[i].iov_len) {
502
            /* skip this sg */
503
            offset -= sg[i].iov_len;
504
            continue;
505
        } else {
506
            len = MIN(sg[i].iov_len - offset, size);
507
            if (pack) {
508
                memcpy(sg[i].iov_base + offset, addr, len);
509
            } else {
510
                memcpy(addr, sg[i].iov_base + offset, len);
511
            }
512
            size -= len;
513
            copied += len;
514
            addr += len;
515
            if (size) {
516
                offset = 0;
517
                continue;
518
            }
519
        }
520
    }
521

    
522
    return copied;
523
}
524

    
525
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
526
{
527
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
528
                         offset, size, 0);
529
}
530

    
531
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
532
                        size_t size)
533
{
534
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
535
                             offset, size, 1);
536
}
537

    
538
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
539
{
540
    size_t pos = 0;
541
    int i, j;
542
    struct iovec *src_sg;
543
    unsigned int num;
544

    
545
    if (rx) {
546
        src_sg = pdu->elem.in_sg;
547
        num = pdu->elem.in_num;
548
    } else {
549
        src_sg = pdu->elem.out_sg;
550
        num = pdu->elem.out_num;
551
    }
552

    
553
    j = 0;
554
    for (i = 0; i < num; i++) {
555
        if (offset <= pos) {
556
            sg[j].iov_base = src_sg[i].iov_base;
557
            sg[j].iov_len = src_sg[i].iov_len;
558
            j++;
559
        } else if (offset < (src_sg[i].iov_len + pos)) {
560
            sg[j].iov_base = src_sg[i].iov_base;
561
            sg[j].iov_len = src_sg[i].iov_len;
562
            sg[j].iov_base += (offset - pos);
563
            sg[j].iov_len -= (offset - pos);
564
            j++;
565
        }
566
        pos += src_sg[i].iov_len;
567
    }
568

    
569
    return j;
570
}
571

    
572
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
573
{
574
    size_t old_offset = offset;
575
    va_list ap;
576
    int i;
577

    
578
    va_start(ap, fmt);
579
    for (i = 0; fmt[i]; i++) {
580
        switch (fmt[i]) {
581
        case 'b': {
582
            uint8_t *valp = va_arg(ap, uint8_t *);
583
            offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
584
            break;
585
        }
586
        case 'w': {
587
            uint16_t val, *valp;
588
            valp = va_arg(ap, uint16_t *);
589
            val = le16_to_cpupu(valp);
590
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
591
            *valp = val;
592
            break;
593
        }
594
        case 'd': {
595
            uint32_t val, *valp;
596
            valp = va_arg(ap, uint32_t *);
597
            val = le32_to_cpupu(valp);
598
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
599
            *valp = val;
600
            break;
601
        }
602
        case 'q': {
603
            uint64_t val, *valp;
604
            valp = va_arg(ap, uint64_t *);
605
            val = le64_to_cpup(valp);
606
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
607
            *valp = val;
608
            break;
609
        }
610
        case 'v': {
611
            struct iovec *iov = va_arg(ap, struct iovec *);
612
            int *iovcnt = va_arg(ap, int *);
613
            *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
614
            break;
615
        }
616
        case 's': {
617
            V9fsString *str = va_arg(ap, V9fsString *);
618
            offset += pdu_unmarshal(pdu, offset, "w", &str->size);
619
            /* FIXME: sanity check str->size */
620
            str->data = qemu_malloc(str->size + 1);
621
            offset += pdu_unpack(str->data, pdu, offset, str->size);
622
            str->data[str->size] = 0;
623
            break;
624
        }
625
        case 'Q': {
626
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
627
            offset += pdu_unmarshal(pdu, offset, "bdq",
628
                        &qidp->type, &qidp->version, &qidp->path);
629
            break;
630
        }
631
        case 'S': {
632
            V9fsStat *statp = va_arg(ap, V9fsStat *);
633
            offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
634
                        &statp->size, &statp->type, &statp->dev,
635
                        &statp->qid, &statp->mode, &statp->atime,
636
                        &statp->mtime, &statp->length,
637
                        &statp->name, &statp->uid, &statp->gid,
638
                        &statp->muid, &statp->extension,
639
                        &statp->n_uid, &statp->n_gid,
640
                        &statp->n_muid);
641
            break;
642
        }
643
        default:
644
            break;
645
        }
646
    }
647

    
648
    va_end(ap);
649

    
650
    return offset - old_offset;
651
}
652

    
653
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
654
{
655
    size_t old_offset = offset;
656
    va_list ap;
657
    int i;
658

    
659
    va_start(ap, fmt);
660
    for (i = 0; fmt[i]; i++) {
661
        switch (fmt[i]) {
662
        case 'b': {
663
            uint8_t val = va_arg(ap, int);
664
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
665
            break;
666
        }
667
        case 'w': {
668
            uint16_t val;
669
            cpu_to_le16w(&val, va_arg(ap, int));
670
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
671
            break;
672
        }
673
        case 'd': {
674
            uint32_t val;
675
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
676
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
677
            break;
678
        }
679
        case 'q': {
680
            uint64_t val;
681
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
682
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
683
            break;
684
        }
685
        case 'v': {
686
            struct iovec *iov = va_arg(ap, struct iovec *);
687
            int *iovcnt = va_arg(ap, int *);
688
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
689
            break;
690
        }
691
        case 's': {
692
            V9fsString *str = va_arg(ap, V9fsString *);
693
            offset += pdu_marshal(pdu, offset, "w", str->size);
694
            offset += pdu_pack(pdu, offset, str->data, str->size);
695
            break;
696
        }
697
        case 'Q': {
698
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
699
            offset += pdu_marshal(pdu, offset, "bdq",
700
                        qidp->type, qidp->version, qidp->path);
701
            break;
702
        }
703
        case 'S': {
704
            V9fsStat *statp = va_arg(ap, V9fsStat *);
705
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
706
                        statp->size, statp->type, statp->dev,
707
                        &statp->qid, statp->mode, statp->atime,
708
                        statp->mtime, statp->length, &statp->name,
709
                        &statp->uid, &statp->gid, &statp->muid,
710
                        &statp->extension, statp->n_uid,
711
                        statp->n_gid, statp->n_muid);
712
            break;
713
        }
714
        default:
715
            break;
716
        }
717
    }
718
    va_end(ap);
719

    
720
    return offset - old_offset;
721
}
722

    
723
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
724
{
725
    int8_t id = pdu->id + 1; /* Response */
726

    
727
    if (len < 0) {
728
        V9fsString str;
729
        int err = -len;
730

    
731
        str.data = strerror(err);
732
        str.size = strlen(str.data);
733

    
734
        len = 7;
735
        len += pdu_marshal(pdu, len, "s", &str);
736
        if (dotu) {
737
            len += pdu_marshal(pdu, len, "d", err);
738
        }
739

    
740
        id = P9_RERROR;
741
    }
742

    
743
    /* fill out the header */
744
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
745

    
746
    /* keep these in sync */
747
    pdu->size = len;
748
    pdu->id = id;
749

    
750
    /* push onto queue and notify */
751
    virtqueue_push(s->vq, &pdu->elem, len);
752

    
753
    /* FIXME: we should batch these completions */
754
    virtio_notify(&s->vdev, s->vq);
755

    
756
    free_pdu(s, pdu);
757
}
758

    
759
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
760
{
761
    mode_t ret;
762

    
763
    ret = mode & 0777;
764
    if (mode & P9_STAT_MODE_DIR) {
765
        ret |= S_IFDIR;
766
    }
767

    
768
    if (dotu) {
769
        if (mode & P9_STAT_MODE_SYMLINK) {
770
            ret |= S_IFLNK;
771
        }
772
        if (mode & P9_STAT_MODE_SOCKET) {
773
            ret |= S_IFSOCK;
774
        }
775
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
776
            ret |= S_IFIFO;
777
        }
778
        if (mode & P9_STAT_MODE_DEVICE) {
779
            if (extension && extension->data[0] == 'c') {
780
                ret |= S_IFCHR;
781
            } else {
782
                ret |= S_IFBLK;
783
            }
784
        }
785
    }
786

    
787
    if (!(ret&~0777)) {
788
        ret |= S_IFREG;
789
    }
790

    
791
    if (mode & P9_STAT_MODE_SETUID) {
792
        ret |= S_ISUID;
793
    }
794
    if (mode & P9_STAT_MODE_SETGID) {
795
        ret |= S_ISGID;
796
    }
797
    if (mode & P9_STAT_MODE_SETVTX) {
798
        ret |= S_ISVTX;
799
    }
800

    
801
    return ret;
802
}
803

    
804
static int donttouch_stat(V9fsStat *stat)
805
{
806
    if (stat->type == -1 &&
807
        stat->dev == -1 &&
808
        stat->qid.type == -1 &&
809
        stat->qid.version == -1 &&
810
        stat->qid.path == -1 &&
811
        stat->mode == -1 &&
812
        stat->atime == -1 &&
813
        stat->mtime == -1 &&
814
        stat->length == -1 &&
815
        !stat->name.size &&
816
        !stat->uid.size &&
817
        !stat->gid.size &&
818
        !stat->muid.size &&
819
        stat->n_uid == -1 &&
820
        stat->n_gid == -1 &&
821
        stat->n_muid == -1) {
822
        return 1;
823
    }
824

    
825
    return 0;
826
}
827

    
828
static void v9fs_stat_free(V9fsStat *stat)
829
{
830
    v9fs_string_free(&stat->name);
831
    v9fs_string_free(&stat->uid);
832
    v9fs_string_free(&stat->gid);
833
    v9fs_string_free(&stat->muid);
834
    v9fs_string_free(&stat->extension);
835
}
836

    
837
static uint32_t stat_to_v9mode(const struct stat *stbuf)
838
{
839
    uint32_t mode;
840

    
841
    mode = stbuf->st_mode & 0777;
842
    if (S_ISDIR(stbuf->st_mode)) {
843
        mode |= P9_STAT_MODE_DIR;
844
    }
845

    
846
    if (dotu) {
847
        if (S_ISLNK(stbuf->st_mode)) {
848
            mode |= P9_STAT_MODE_SYMLINK;
849
        }
850

    
851
        if (S_ISSOCK(stbuf->st_mode)) {
852
            mode |= P9_STAT_MODE_SOCKET;
853
        }
854

    
855
        if (S_ISFIFO(stbuf->st_mode)) {
856
            mode |= P9_STAT_MODE_NAMED_PIPE;
857
        }
858

    
859
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
860
            mode |= P9_STAT_MODE_DEVICE;
861
        }
862

    
863
        if (stbuf->st_mode & S_ISUID) {
864
            mode |= P9_STAT_MODE_SETUID;
865
        }
866

    
867
        if (stbuf->st_mode & S_ISGID) {
868
            mode |= P9_STAT_MODE_SETGID;
869
        }
870

    
871
        if (stbuf->st_mode & S_ISVTX) {
872
            mode |= P9_STAT_MODE_SETVTX;
873
        }
874
    }
875

    
876
    return mode;
877
}
878

    
879
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
880
                            const struct stat *stbuf,
881
                            V9fsStat *v9stat)
882
{
883
    int err;
884
    const char *str;
885

    
886
    memset(v9stat, 0, sizeof(*v9stat));
887

    
888
    stat_to_qid(stbuf, &v9stat->qid);
889
    v9stat->mode = stat_to_v9mode(stbuf);
890
    v9stat->atime = stbuf->st_atime;
891
    v9stat->mtime = stbuf->st_mtime;
892
    v9stat->length = stbuf->st_size;
893

    
894
    v9fs_string_null(&v9stat->uid);
895
    v9fs_string_null(&v9stat->gid);
896
    v9fs_string_null(&v9stat->muid);
897

    
898
    if (dotu) {
899
        v9stat->n_uid = stbuf->st_uid;
900
        v9stat->n_gid = stbuf->st_gid;
901
        v9stat->n_muid = 0;
902

    
903
        v9fs_string_null(&v9stat->extension);
904

    
905
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
906
            err = v9fs_do_readlink(s, name, &v9stat->extension);
907
            if (err == -1) {
908
                err = -errno;
909
                return err;
910
            }
911
            v9stat->extension.data[err] = 0;
912
            v9stat->extension.size = err;
913
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
914
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
915
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
916
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
917
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
918
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
919
                    "HARDLINKCOUNT", stbuf->st_nlink);
920
        }
921
    }
922

    
923
    str = strrchr(name->data, '/');
924
    if (str) {
925
        str += 1;
926
    } else {
927
        str = name->data;
928
    }
929

    
930
    v9fs_string_sprintf(&v9stat->name, "%s", str);
931

    
932
    v9stat->size = 61 +
933
        v9fs_string_size(&v9stat->name) +
934
        v9fs_string_size(&v9stat->uid) +
935
        v9fs_string_size(&v9stat->gid) +
936
        v9fs_string_size(&v9stat->muid) +
937
        v9fs_string_size(&v9stat->extension);
938
    return 0;
939
}
940

    
941
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
942
{
943
    while (len && *iovcnt) {
944
        if (len < sg->iov_len) {
945
            sg->iov_len -= len;
946
            sg->iov_base += len;
947
            len = 0;
948
        } else {
949
            len -= sg->iov_len;
950
            sg++;
951
            *iovcnt -= 1;
952
        }
953
    }
954

    
955
    return sg;
956
}
957

    
958
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
959
{
960
    int i;
961
    int total = 0;
962

    
963
    for (i = 0; i < *cnt; i++) {
964
        if ((total + sg[i].iov_len) > cap) {
965
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
966
            i++;
967
            break;
968
        }
969
        total += sg[i].iov_len;
970
    }
971

    
972
    *cnt = i;
973

    
974
    return sg;
975
}
976

    
977
static void print_sg(struct iovec *sg, int cnt)
978
{
979
    int i;
980

    
981
    printf("sg[%d]: {", cnt);
982
    for (i = 0; i < cnt; i++) {
983
        if (i) {
984
            printf(", ");
985
        }
986
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
987
    }
988
    printf("}\n");
989
}
990

    
991
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
992
{
993
    V9fsString str;
994
    v9fs_string_init(&str);
995
    v9fs_string_copy(&str, dst);
996
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
997
    v9fs_string_free(&str);
998
}
999

    
1000
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1001
{
1002
    int32_t msize;
1003
    V9fsString version;
1004
    size_t offset = 7;
1005

    
1006
    pdu_unmarshal(pdu, offset, "ds", &msize, &version);
1007

    
1008
    if (strcmp(version.data, "9P2000.u")) {
1009
        v9fs_string_sprintf(&version, "unknown");
1010
    }
1011

    
1012
    offset += pdu_marshal(pdu, offset, "ds", msize, &version);
1013
    complete_pdu(s, pdu, offset);
1014

    
1015
    v9fs_string_free(&version);
1016
}
1017

    
1018
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1019
{
1020
    int32_t fid, afid, n_uname;
1021
    V9fsString uname, aname;
1022
    V9fsFidState *fidp;
1023
    V9fsQID qid;
1024
    size_t offset = 7;
1025
    ssize_t err;
1026

    
1027
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1028

    
1029
    fidp = alloc_fid(s, fid);
1030
    if (fidp == NULL) {
1031
        err = -EINVAL;
1032
        goto out;
1033
    }
1034

    
1035
    fidp->uid = n_uname;
1036

    
1037
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1038
    err = fid_to_qid(s, fidp, &qid);
1039
    if (err) {
1040
        err = -EINVAL;
1041
        free_fid(s, fid);
1042
        goto out;
1043
    }
1044

    
1045
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1046

    
1047
    err = offset;
1048
out:
1049
    complete_pdu(s, pdu, err);
1050
    v9fs_string_free(&uname);
1051
    v9fs_string_free(&aname);
1052
}
1053

    
1054
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1055
{
1056
    if (err == -1) {
1057
        err = -errno;
1058
        goto out;
1059
    }
1060

    
1061
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1062
    if (err) {
1063
        goto out;
1064
    }
1065
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1066
    err = vs->offset;
1067

    
1068
out:
1069
    complete_pdu(s, vs->pdu, err);
1070
    v9fs_stat_free(&vs->v9stat);
1071
    qemu_free(vs);
1072
}
1073

    
1074
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1075
{
1076
    int32_t fid;
1077
    V9fsStatState *vs;
1078
    ssize_t err = 0;
1079

    
1080
    vs = qemu_malloc(sizeof(*vs));
1081
    vs->pdu = pdu;
1082
    vs->offset = 7;
1083

    
1084
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1085

    
1086
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1087

    
1088
    vs->fidp = lookup_fid(s, fid);
1089
    if (vs->fidp == NULL) {
1090
        err = -ENOENT;
1091
        goto out;
1092
    }
1093

    
1094
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1095
    v9fs_stat_post_lstat(s, vs, err);
1096
    return;
1097

    
1098
out:
1099
    complete_pdu(s, vs->pdu, err);
1100
    v9fs_stat_free(&vs->v9stat);
1101
    qemu_free(vs);
1102
}
1103

    
1104
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1105
{
1106
    complete_pdu(s, vs->pdu, err);
1107

    
1108
    if (vs->nwnames) {
1109
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1110
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1111
        }
1112

    
1113
        qemu_free(vs->wnames);
1114
        qemu_free(vs->qids);
1115
    }
1116
}
1117

    
1118
static void v9fs_walk_marshal(V9fsWalkState *vs)
1119
{
1120
    int i;
1121
    vs->offset = 7;
1122
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1123

    
1124
    for (i = 0; i < vs->nwnames; i++) {
1125
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1126
    }
1127
}
1128

    
1129
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1130
                                                                int err)
1131
{
1132
    if (err == -1) {
1133
        free_fid(s, vs->newfidp->fid);
1134
        v9fs_string_free(&vs->path);
1135
        err = -ENOENT;
1136
        goto out;
1137
    }
1138

    
1139
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1140

    
1141
    vs->name_idx++;
1142
    if (vs->name_idx < vs->nwnames) {
1143
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1144
                                            vs->wnames[vs->name_idx].data);
1145
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1146

    
1147
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1148
        v9fs_walk_post_newfid_lstat(s, vs, err);
1149
        return;
1150
    }
1151

    
1152
    v9fs_string_free(&vs->path);
1153
    v9fs_walk_marshal(vs);
1154
    err = vs->offset;
1155
out:
1156
    v9fs_walk_complete(s, vs, err);
1157
}
1158

    
1159
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1160
        int err)
1161
{
1162
    if (err == -1) {
1163
        v9fs_string_free(&vs->path);
1164
        err = -ENOENT;
1165
        goto out;
1166
    }
1167

    
1168
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1169
    vs->name_idx++;
1170
    if (vs->name_idx < vs->nwnames) {
1171

    
1172
        v9fs_string_sprintf(&vs->path, "%s/%s",
1173
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1174
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1175

    
1176
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1177
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1178
        return;
1179
    }
1180

    
1181
    v9fs_string_free(&vs->path);
1182
    v9fs_walk_marshal(vs);
1183
    err = vs->offset;
1184
out:
1185
    v9fs_walk_complete(s, vs, err);
1186
}
1187

    
1188
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1189
{
1190
    int32_t fid, newfid;
1191
    V9fsWalkState *vs;
1192
    int err = 0;
1193
    int i;
1194

    
1195
    vs = qemu_malloc(sizeof(*vs));
1196
    vs->pdu = pdu;
1197
    vs->wnames = NULL;
1198
    vs->qids = NULL;
1199
    vs->offset = 7;
1200

    
1201
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1202
                                            &newfid, &vs->nwnames);
1203

    
1204
    if (vs->nwnames) {
1205
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1206

    
1207
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1208

    
1209
        for (i = 0; i < vs->nwnames; i++) {
1210
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1211
                                            &vs->wnames[i]);
1212
        }
1213
    }
1214

    
1215
    vs->fidp = lookup_fid(s, fid);
1216
    if (vs->fidp == NULL) {
1217
        err = -ENOENT;
1218
        goto out;
1219
    }
1220

    
1221
    /* FIXME: is this really valid? */
1222
    if (fid == newfid) {
1223

    
1224
        BUG_ON(vs->fidp->fd != -1);
1225
        BUG_ON(vs->fidp->dir);
1226
        v9fs_string_init(&vs->path);
1227
        vs->name_idx = 0;
1228

    
1229
        if (vs->name_idx < vs->nwnames) {
1230
            v9fs_string_sprintf(&vs->path, "%s/%s",
1231
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1232
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1233

    
1234
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1235
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1236
            return;
1237
        }
1238
    } else {
1239
        vs->newfidp = alloc_fid(s, newfid);
1240
        if (vs->newfidp == NULL) {
1241
            err = -EINVAL;
1242
            goto out;
1243
        }
1244

    
1245
        vs->newfidp->uid = vs->fidp->uid;
1246
        v9fs_string_init(&vs->path);
1247
        vs->name_idx = 0;
1248
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1249

    
1250
        if (vs->name_idx < vs->nwnames) {
1251
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1252
                                vs->wnames[vs->name_idx].data);
1253
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1254

    
1255
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1256
            v9fs_walk_post_newfid_lstat(s, vs, err);
1257
            return;
1258
        }
1259
    }
1260

    
1261
    v9fs_walk_marshal(vs);
1262
    err = vs->offset;
1263
out:
1264
    v9fs_walk_complete(s, vs, err);
1265
}
1266

    
1267
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1268
{
1269
    if (vs->fidp->dir == NULL) {
1270
        err = -errno;
1271
        goto out;
1272
    }
1273

    
1274
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1275
    err = vs->offset;
1276
out:
1277
    complete_pdu(s, vs->pdu, err);
1278
    qemu_free(vs);
1279

    
1280
}
1281

    
1282
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1283
{
1284
    if (vs->fidp->fd == -1) {
1285
        err = -errno;
1286
        goto out;
1287
    }
1288

    
1289
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1290
    err = vs->offset;
1291
out:
1292
    complete_pdu(s, vs->pdu, err);
1293
    qemu_free(vs);
1294
}
1295

    
1296
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1297
{
1298
    if (err) {
1299
        err = -errno;
1300
        goto out;
1301
    }
1302

    
1303
    stat_to_qid(&vs->stbuf, &vs->qid);
1304

    
1305
    if (S_ISDIR(vs->stbuf.st_mode)) {
1306
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1307
        v9fs_open_post_opendir(s, vs, err);
1308
    } else {
1309
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1310
                                    omode_to_uflags(vs->mode));
1311
        v9fs_open_post_open(s, vs, err);
1312
    }
1313
    return;
1314
out:
1315
    complete_pdu(s, vs->pdu, err);
1316
    qemu_free(vs);
1317
}
1318

    
1319
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1320
{
1321
    int32_t fid;
1322
    V9fsOpenState *vs;
1323
    ssize_t err = 0;
1324

    
1325

    
1326
    vs = qemu_malloc(sizeof(*vs));
1327
    vs->pdu = pdu;
1328
    vs->offset = 7;
1329

    
1330
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1331

    
1332
    vs->fidp = lookup_fid(s, fid);
1333
    if (vs->fidp == NULL) {
1334
        err = -ENOENT;
1335
        goto out;
1336
    }
1337

    
1338
    BUG_ON(vs->fidp->fd != -1);
1339
    BUG_ON(vs->fidp->dir);
1340

    
1341
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1342

    
1343
    v9fs_open_post_lstat(s, vs, err);
1344
    return;
1345
out:
1346
    complete_pdu(s, pdu, err);
1347
    qemu_free(vs);
1348
}
1349

    
1350
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1351
{
1352
    int32_t fid;
1353
    size_t offset = 7;
1354
    int err;
1355

    
1356
    pdu_unmarshal(pdu, offset, "d", &fid);
1357

    
1358
    err = free_fid(s, fid);
1359
    if (err < 0) {
1360
        goto out;
1361
    }
1362

    
1363
    offset = 7;
1364
    err = offset;
1365
out:
1366
    complete_pdu(s, pdu, err);
1367
}
1368

    
1369
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1370

    
1371
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1372
{
1373
    if (err) {
1374
        goto out;
1375
    }
1376
    v9fs_stat_free(&vs->v9stat);
1377
    v9fs_string_free(&vs->name);
1378
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1379
    vs->offset += vs->count;
1380
    err = vs->offset;
1381
out:
1382
    complete_pdu(s, vs->pdu, err);
1383
    qemu_free(vs);
1384
    return;
1385
}
1386

    
1387
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1388
                                    ssize_t err)
1389
{
1390
    if (err) {
1391
        err = -errno;
1392
        goto out;
1393
    }
1394
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1395
    if (err) {
1396
        goto out;
1397
    }
1398

    
1399
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1400
                            &vs->v9stat);
1401
    if ((vs->len != (vs->v9stat.size + 2)) ||
1402
            ((vs->count + vs->len) > vs->max_count)) {
1403
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1404
        v9fs_read_post_seekdir(s, vs, err);
1405
        return;
1406
    }
1407
    vs->count += vs->len;
1408
    v9fs_stat_free(&vs->v9stat);
1409
    v9fs_string_free(&vs->name);
1410
    vs->dir_pos = vs->dent->d_off;
1411
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1412
    v9fs_read_post_readdir(s, vs, err);
1413
    return;
1414
out:
1415
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1416
    v9fs_read_post_seekdir(s, vs, err);
1417
    return;
1418

    
1419
}
1420

    
1421
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1422
{
1423
    if (vs->dent) {
1424
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1425
        v9fs_string_init(&vs->name);
1426
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1427
                            vs->dent->d_name);
1428
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1429
        v9fs_read_post_dir_lstat(s, vs, err);
1430
        return;
1431
    }
1432

    
1433
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1434
    vs->offset += vs->count;
1435
    err = vs->offset;
1436
    complete_pdu(s, vs->pdu, err);
1437
    qemu_free(vs);
1438
    return;
1439
}
1440

    
1441
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1442
{
1443
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1444
    v9fs_read_post_readdir(s, vs, err);
1445
    return;
1446
}
1447

    
1448
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1449
                                       ssize_t err)
1450
{
1451
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1452
    v9fs_read_post_telldir(s, vs, err);
1453
    return;
1454
}
1455

    
1456
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1457
{
1458
    if (err  < 0) {
1459
        /* IO error return the error */
1460
        err = -errno;
1461
        goto out;
1462
    }
1463
    vs->total += vs->len;
1464
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1465
    if (vs->total < vs->count && vs->len > 0) {
1466
        do {
1467
            if (0) {
1468
                print_sg(vs->sg, vs->cnt);
1469
            }
1470
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1471
        } while (vs->len == -1 && errno == EINTR);
1472
        if (vs->len == -1) {
1473
            err  = -errno;
1474
        }
1475
        v9fs_read_post_readv(s, vs, err);
1476
        return;
1477
    }
1478
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1479
    vs->offset += vs->count;
1480
    err = vs->offset;
1481

    
1482
out:
1483
    complete_pdu(s, vs->pdu, err);
1484
    qemu_free(vs);
1485
}
1486

    
1487
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1488
{
1489
    if (err == -1) {
1490
        err = -errno;
1491
        goto out;
1492
    }
1493
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1494

    
1495
    if (vs->total < vs->count) {
1496
        do {
1497
            if (0) {
1498
                print_sg(vs->sg, vs->cnt);
1499
            }
1500
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1501
        } while (vs->len == -1 && errno == EINTR);
1502
        if (vs->len == -1) {
1503
            err  = -errno;
1504
        }
1505
        v9fs_read_post_readv(s, vs, err);
1506
        return;
1507
    }
1508
out:
1509
    complete_pdu(s, vs->pdu, err);
1510
    qemu_free(vs);
1511
}
1512

    
1513
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1514
{
1515
    int32_t fid;
1516
    V9fsReadState *vs;
1517
    ssize_t err = 0;
1518

    
1519
    vs = qemu_malloc(sizeof(*vs));
1520
    vs->pdu = pdu;
1521
    vs->offset = 7;
1522
    vs->total = 0;
1523
    vs->len = 0;
1524
    vs->count = 0;
1525

    
1526
    pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1527

    
1528
    vs->fidp = lookup_fid(s, fid);
1529
    if (vs->fidp == NULL) {
1530
        err = -EINVAL;
1531
        goto out;
1532
    }
1533

    
1534
    if (vs->fidp->dir) {
1535
        vs->max_count = vs->count;
1536
        vs->count = 0;
1537
        if (vs->off == 0) {
1538
            v9fs_do_rewinddir(s, vs->fidp->dir);
1539
        }
1540
        v9fs_read_post_rewinddir(s, vs, err);
1541
        return;
1542
    } else if (vs->fidp->fd != -1) {
1543
        vs->sg = vs->iov;
1544
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1545
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1546
        v9fs_read_post_lseek(s, vs, err);
1547
        return;
1548
    } else {
1549
        err = -EINVAL;
1550
    }
1551
out:
1552
    complete_pdu(s, pdu, err);
1553
    qemu_free(vs);
1554
}
1555

    
1556
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1557
                                   ssize_t err)
1558
{
1559
    if (err  < 0) {
1560
        /* IO error return the error */
1561
        err = -errno;
1562
        goto out;
1563
    }
1564
    vs->total += vs->len;
1565
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1566
    if (vs->total < vs->count && vs->len > 0) {
1567
        do {
1568
            if (0) {
1569
                print_sg(vs->sg, vs->cnt);
1570
            }
1571
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1572
        } while (vs->len == -1 && errno == EINTR);
1573
        if (vs->len == -1) {
1574
            err  = -errno;
1575
        }
1576
        v9fs_write_post_writev(s, vs, err);
1577
        return;
1578
    }
1579
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1580

    
1581
    err = vs->offset;
1582
out:
1583
    complete_pdu(s, vs->pdu, err);
1584
    qemu_free(vs);
1585
}
1586

    
1587
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1588
{
1589
    if (err == -1) {
1590
        err = -errno;
1591
        goto out;
1592
    }
1593
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1594

    
1595
    if (vs->total < vs->count) {
1596
        do {
1597
            if (0) {
1598
                print_sg(vs->sg, vs->cnt);
1599
            }
1600
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1601
        } while (vs->len == -1 && errno == EINTR);
1602
        if (vs->len == -1) {
1603
            err  = -errno;
1604
        }
1605
        v9fs_write_post_writev(s, vs, err);
1606
        return;
1607
    }
1608

    
1609
out:
1610
    complete_pdu(s, vs->pdu, err);
1611
    qemu_free(vs);
1612
}
1613

    
1614
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1615
{
1616
    int32_t fid;
1617
    V9fsWriteState *vs;
1618
    ssize_t err;
1619

    
1620
    vs = qemu_malloc(sizeof(*vs));
1621

    
1622
    vs->pdu = pdu;
1623
    vs->offset = 7;
1624
    vs->sg = vs->iov;
1625
    vs->total = 0;
1626
    vs->len = 0;
1627

    
1628
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1629
                    vs->sg, &vs->cnt);
1630

    
1631
    vs->fidp = lookup_fid(s, fid);
1632
    if (vs->fidp == NULL) {
1633
        err = -EINVAL;
1634
        goto out;
1635
    }
1636

    
1637
    if (vs->fidp->fd == -1) {
1638
        err = -EINVAL;
1639
        goto out;
1640
    }
1641

    
1642
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1643

    
1644
    v9fs_write_post_lseek(s, vs, err);
1645
    return;
1646

    
1647
out:
1648
    complete_pdu(s, vs->pdu, err);
1649
    qemu_free(vs);
1650
}
1651

    
1652
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1653
{
1654
    if (err == 0) {
1655
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1656
        stat_to_qid(&vs->stbuf, &vs->qid);
1657

    
1658
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1659

    
1660
        err = vs->offset;
1661
    }
1662

    
1663
    complete_pdu(s, vs->pdu, err);
1664
    v9fs_string_free(&vs->name);
1665
    v9fs_string_free(&vs->extension);
1666
    v9fs_string_free(&vs->fullname);
1667
    qemu_free(vs);
1668
}
1669

    
1670
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1671
{
1672
    if (err) {
1673
        err = -errno;
1674
    }
1675
    v9fs_post_create(s, vs, err);
1676
}
1677

    
1678
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1679
                                                                    int err)
1680
{
1681
    if (!vs->fidp->dir) {
1682
        err = -errno;
1683
    }
1684
    v9fs_post_create(s, vs, err);
1685
}
1686

    
1687
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1688
                                                                    int err)
1689
{
1690
    if (err) {
1691
        err = -errno;
1692
        goto out;
1693
    }
1694

    
1695
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1696
    v9fs_create_post_opendir(s, vs, err);
1697
    return;
1698

    
1699
out:
1700
    v9fs_post_create(s, vs, err);
1701
}
1702

    
1703
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1704
{
1705
    if (err) {
1706
        err = -errno;
1707
        goto out;
1708
    }
1709

    
1710
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1711
    v9fs_create_post_dir_lstat(s, vs, err);
1712
    return;
1713

    
1714
out:
1715
    v9fs_post_create(s, vs, err);
1716
}
1717

    
1718
static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1719
                                                                int err)
1720
{
1721
    if (err) {
1722
        err = -errno;
1723
        goto out;
1724
    }
1725

    
1726
    err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1727
    v9fs_create_post_perms(s, vs, err);
1728
    return;
1729

    
1730
out:
1731
    v9fs_post_create(s, vs, err);
1732
}
1733

    
1734
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1735
{
1736
    if (err) {
1737
        vs->fidp->fd = -1;
1738
        err = -errno;
1739
    }
1740

    
1741
    v9fs_post_create(s, vs, err);
1742
    return;
1743
}
1744

    
1745
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1746
{
1747
    if (vs->fidp->fd == -1) {
1748
        err = -errno;
1749
        goto out;
1750
    }
1751

    
1752
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1753
    v9fs_create_post_fstat(s, vs, err);
1754

    
1755
    return;
1756

    
1757
out:
1758
    v9fs_post_create(s, vs, err);
1759

    
1760
}
1761

    
1762
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1763
{
1764

    
1765
    if (err == 0 || errno != ENOENT) {
1766
        err = -errno;
1767
        goto out;
1768
    }
1769

    
1770
    if (vs->perm & P9_STAT_MODE_DIR) {
1771
        err = v9fs_do_mkdir(s, &vs->fullname, vs->perm & 0777);
1772
        v9fs_create_post_mkdir(s, vs, err);
1773
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
1774
        err = v9fs_do_symlink(s, &vs->extension, &vs->fullname);
1775
        v9fs_create_post_perms(s, vs, err);
1776
    } else if (vs->perm & P9_STAT_MODE_LINK) {
1777
        int32_t nfid = atoi(vs->extension.data);
1778
        V9fsFidState *nfidp = lookup_fid(s, nfid);
1779
        if (nfidp == NULL) {
1780
            err = -errno;
1781
            v9fs_post_create(s, vs, err);
1782
        }
1783
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1784
        v9fs_create_post_perms(s, vs, err);
1785
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1786
        char ctype;
1787
        uint32_t major, minor;
1788
        mode_t nmode = 0;
1789

    
1790
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1791
                                        &minor) != 3) {
1792
            err = -errno;
1793
            v9fs_post_create(s, vs, err);
1794
        }
1795

    
1796
        switch (ctype) {
1797
        case 'c':
1798
            nmode = S_IFCHR;
1799
            break;
1800
        case 'b':
1801
            nmode = S_IFBLK;
1802
            break;
1803
        default:
1804
            err = -EIO;
1805
            v9fs_post_create(s, vs, err);
1806
        }
1807

    
1808
        nmode |= vs->perm & 0777;
1809
        err = v9fs_do_mknod(s, &vs->fullname, nmode, makedev(major, minor));
1810
        v9fs_create_post_perms(s, vs, err);
1811
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1812
        err = v9fs_do_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
1813
        v9fs_post_create(s, vs, err);
1814
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1815
        err = v9fs_do_mksock(s, &vs->fullname);
1816
        v9fs_create_post_mksock(s, vs, err);
1817
    } else {
1818
        vs->fidp->fd = v9fs_do_open2(s, &vs->fullname,
1819
                                omode_to_uflags(vs->mode) | O_CREAT,
1820
                                vs->perm & 0777);
1821
        v9fs_create_post_open2(s, vs, err);
1822
    }
1823

    
1824
    return;
1825

    
1826
out:
1827
    v9fs_post_create(s, vs, err);
1828
}
1829

    
1830
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1831
{
1832
    int32_t fid;
1833
    V9fsCreateState *vs;
1834
    int err = 0;
1835

    
1836
    vs = qemu_malloc(sizeof(*vs));
1837
    vs->pdu = pdu;
1838
    vs->offset = 7;
1839

    
1840
    v9fs_string_init(&vs->fullname);
1841

    
1842
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1843
                                &vs->perm, &vs->mode, &vs->extension);
1844

    
1845
    vs->fidp = lookup_fid(s, fid);
1846
    if (vs->fidp == NULL) {
1847
        err = -EINVAL;
1848
        goto out;
1849
    }
1850

    
1851
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1852
                                                        vs->name.data);
1853

    
1854
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1855
    v9fs_create_post_lstat(s, vs, err);
1856
    return;
1857

    
1858
out:
1859
    complete_pdu(s, vs->pdu, err);
1860
    v9fs_string_free(&vs->name);
1861
    v9fs_string_free(&vs->extension);
1862
    qemu_free(vs);
1863
}
1864

    
1865
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1866
{
1867
    /* A nop call with no return */
1868
    complete_pdu(s, pdu, 7);
1869
}
1870

    
1871
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1872
                                                                int err)
1873
{
1874
    /* For TREMOVE we need to clunk the fid even on failed remove */
1875
    err = free_fid(s, vs->fidp->fid);
1876
    if (err < 0) {
1877
        goto out;
1878
    }
1879

    
1880
    err = vs->offset;
1881
out:
1882
    complete_pdu(s, vs->pdu, err);
1883
    qemu_free(vs);
1884
}
1885

    
1886
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1887
{
1888
    int32_t fid;
1889
    V9fsRemoveState *vs;
1890
    int err = 0;
1891

    
1892
    vs = qemu_malloc(sizeof(*vs));
1893
    vs->pdu = pdu;
1894
    vs->offset = 7;
1895

    
1896
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1897

    
1898
    vs->fidp = lookup_fid(s, fid);
1899
    if (vs->fidp == NULL) {
1900
        err = -EINVAL;
1901
        goto out;
1902
    }
1903

    
1904
    err = v9fs_do_remove(s, &vs->fidp->path);
1905
    v9fs_remove_post_remove(s, vs, err);
1906
    return;
1907

    
1908
out:
1909
    complete_pdu(s, pdu, err);
1910
    qemu_free(vs);
1911
}
1912

    
1913
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1914
{
1915
    if (err < 0) {
1916
        goto out;
1917
    }
1918

    
1919
    err = vs->offset;
1920

    
1921
out:
1922
    v9fs_stat_free(&vs->v9stat);
1923
    complete_pdu(s, vs->pdu, err);
1924
    qemu_free(vs);
1925
}
1926

    
1927
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
1928
{
1929
    if (err < 0) {
1930
        goto out;
1931
    }
1932

    
1933
    if (vs->v9stat.name.size != 0) {
1934
        v9fs_string_free(&vs->nname);
1935
    }
1936

    
1937
    if (vs->v9stat.length != -1) {
1938
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
1939
            err = -errno;
1940
        }
1941
    }
1942
    v9fs_wstat_post_truncate(s, vs, err);
1943
    return;
1944

    
1945
out:
1946
    v9fs_stat_free(&vs->v9stat);
1947
    complete_pdu(s, vs->pdu, err);
1948
    qemu_free(vs);
1949
}
1950

    
1951
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
1952
{
1953
    V9fsFidState *fidp;
1954
    if (err < 0) {
1955
        goto out;
1956
    }
1957

    
1958
    if (vs->v9stat.name.size != 0) {
1959
        char *old_name, *new_name;
1960
        char *end;
1961

    
1962
        old_name = vs->fidp->path.data;
1963
        end = strrchr(old_name, '/');
1964
        if (end) {
1965
            end++;
1966
        } else {
1967
            end = old_name;
1968
        }
1969

    
1970
        new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
1971

    
1972
        memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
1973
        memcpy(new_name, old_name, end - old_name);
1974
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
1975
                vs->v9stat.name.size);
1976
        vs->nname.data = new_name;
1977
        vs->nname.size = strlen(new_name);
1978

    
1979
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
1980
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
1981
                err = -errno;
1982
            } else {
1983
                /*
1984
                 * Fixup fid's pointing to the old name to
1985
                 * start pointing to the new name
1986
                 */
1987
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
1988

    
1989
                    if (vs->fidp == fidp) {
1990
                        /*
1991
                         * we replace name of this fid towards the end
1992
                         * so that our below strcmp will work
1993
                         */
1994
                        continue;
1995
                    }
1996
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
1997
                                 strlen(vs->fidp->path.data))) {
1998
                        /* replace the name */
1999
                        v9fs_fix_path(&fidp->path, &vs->nname,
2000
                                      strlen(vs->fidp->path.data));
2001
                    }
2002
                }
2003
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2004
            }
2005
        }
2006
    }
2007
    v9fs_wstat_post_rename(s, vs, err);
2008
    return;
2009

    
2010
out:
2011
    v9fs_stat_free(&vs->v9stat);
2012
    complete_pdu(s, vs->pdu, err);
2013
    qemu_free(vs);
2014
}
2015

    
2016
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2017
{
2018
    if (err < 0) {
2019
        goto out;
2020
    }
2021

    
2022
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2023
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2024
                    vs->v9stat.n_gid)) {
2025
            err = -errno;
2026
        }
2027
    }
2028
    v9fs_wstat_post_chown(s, vs, err);
2029
    return;
2030

    
2031
out:
2032
    v9fs_stat_free(&vs->v9stat);
2033
    complete_pdu(s, vs->pdu, err);
2034
    qemu_free(vs);
2035
}
2036

    
2037
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2038
{
2039
    if (err < 0) {
2040
        goto out;
2041
    }
2042

    
2043
    if (vs->v9stat.mtime != -1) {
2044
        struct utimbuf tb;
2045
        tb.actime = 0;
2046
        tb.modtime = vs->v9stat.mtime;
2047
        if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2048
            err = -errno;
2049
        }
2050
    }
2051

    
2052
    v9fs_wstat_post_utime(s, vs, err);
2053
    return;
2054

    
2055
out:
2056
    v9fs_stat_free(&vs->v9stat);
2057
    complete_pdu(s, vs->pdu, err);
2058
    qemu_free(vs);
2059
}
2060

    
2061
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2062
{
2063
    if (err == -1) {
2064
        err = -errno;
2065
    }
2066
    v9fs_stat_free(&vs->v9stat);
2067
    complete_pdu(s, vs->pdu, err);
2068
    qemu_free(vs);
2069
}
2070

    
2071
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2072
{
2073
    uint32_t v9_mode;
2074

    
2075
    if (err == -1) {
2076
        err = -errno;
2077
        goto out;
2078
    }
2079

    
2080
    v9_mode = stat_to_v9mode(&vs->stbuf);
2081

    
2082
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2083
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2084
            /* Attempting to change the type */
2085
            err = -EIO;
2086
            goto out;
2087
    }
2088

    
2089
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2090
                    &vs->v9stat.extension))) {
2091
            err = -errno;
2092
     }
2093
    v9fs_wstat_post_chmod(s, vs, err);
2094
    return;
2095

    
2096
out:
2097
    v9fs_stat_free(&vs->v9stat);
2098
    complete_pdu(s, vs->pdu, err);
2099
    qemu_free(vs);
2100
}
2101

    
2102
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2103
{
2104
    int32_t fid;
2105
    V9fsWstatState *vs;
2106
    int err = 0;
2107

    
2108
    vs = qemu_malloc(sizeof(*vs));
2109
    vs->pdu = pdu;
2110
    vs->offset = 7;
2111

    
2112
    pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2113

    
2114
    vs->fidp = lookup_fid(s, fid);
2115
    if (vs->fidp == NULL) {
2116
        err = -EINVAL;
2117
        goto out;
2118
    }
2119

    
2120
    /* do we need to sync the file? */
2121
    if (donttouch_stat(&vs->v9stat)) {
2122
        err = v9fs_do_fsync(s, vs->fidp->fd);
2123
        v9fs_wstat_post_fsync(s, vs, err);
2124
        return;
2125
    }
2126

    
2127
    if (vs->v9stat.mode != -1) {
2128
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2129
        v9fs_wstat_post_lstat(s, vs, err);
2130
        return;
2131
    }
2132

    
2133
    v9fs_wstat_post_chmod(s, vs, err);
2134
    return;
2135

    
2136
out:
2137
    v9fs_stat_free(&vs->v9stat);
2138
    complete_pdu(s, vs->pdu, err);
2139
    qemu_free(vs);
2140
}
2141

    
2142
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2143

    
2144
static pdu_handler_t *pdu_handlers[] = {
2145
    [P9_TVERSION] = v9fs_version,
2146
    [P9_TATTACH] = v9fs_attach,
2147
    [P9_TSTAT] = v9fs_stat,
2148
    [P9_TWALK] = v9fs_walk,
2149
    [P9_TCLUNK] = v9fs_clunk,
2150
    [P9_TOPEN] = v9fs_open,
2151
    [P9_TREAD] = v9fs_read,
2152
#if 0
2153
    [P9_TAUTH] = v9fs_auth,
2154
#endif
2155
    [P9_TFLUSH] = v9fs_flush,
2156
    [P9_TCREATE] = v9fs_create,
2157
    [P9_TWRITE] = v9fs_write,
2158
    [P9_TWSTAT] = v9fs_wstat,
2159
    [P9_TREMOVE] = v9fs_remove,
2160
};
2161

    
2162
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2163
{
2164
    pdu_handler_t *handler;
2165

    
2166
    if (debug_9p_pdu) {
2167
        pprint_pdu(pdu);
2168
    }
2169

    
2170
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2171

    
2172
    handler = pdu_handlers[pdu->id];
2173
    BUG_ON(handler == NULL);
2174

    
2175
    handler(s, pdu);
2176
}
2177

    
2178
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2179
{
2180
    V9fsState *s = (V9fsState *)vdev;
2181
    V9fsPDU *pdu;
2182
    ssize_t len;
2183

    
2184
    while ((pdu = alloc_pdu(s)) &&
2185
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2186
        uint8_t *ptr;
2187

    
2188
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2189
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2190

    
2191
        ptr = pdu->elem.out_sg[0].iov_base;
2192

    
2193
        memcpy(&pdu->size, ptr, 4);
2194
        pdu->id = ptr[4];
2195
        memcpy(&pdu->tag, ptr + 5, 2);
2196

    
2197
        submit_pdu(s, pdu);
2198
    }
2199

    
2200
    free_pdu(s, pdu);
2201
}
2202

    
2203
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2204
{
2205
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2206
    return features;
2207
}
2208

    
2209
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2210
{
2211
    return (V9fsState *)vdev;
2212
}
2213

    
2214
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2215
{
2216
    struct virtio_9p_config *cfg;
2217
    V9fsState *s = to_virtio_9p(vdev);
2218

    
2219
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2220
                        s->tag_len);
2221
    stw_raw(&cfg->tag_len, s->tag_len);
2222
    memcpy(cfg->tag, s->tag, s->tag_len);
2223
    memcpy(config, cfg, s->config_size);
2224
    qemu_free(cfg);
2225
}
2226

    
2227
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2228
 {
2229
    V9fsState *s;
2230
    int i, len;
2231
    struct stat stat;
2232
    FsTypeEntry *fse;
2233

    
2234

    
2235
    s = (V9fsState *)virtio_common_init("virtio-9p",
2236
                                    VIRTIO_ID_9P,
2237
                                    sizeof(struct virtio_9p_config)+
2238
                                    MAX_TAG_LEN,
2239
                                    sizeof(V9fsState));
2240

    
2241
    /* initialize pdu allocator */
2242
    QLIST_INIT(&s->free_list);
2243
    for (i = 0; i < (MAX_REQ - 1); i++) {
2244
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2245
    }
2246

    
2247
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2248

    
2249
    fse = get_fsdev_fsentry(conf->fsdev_id);
2250

    
2251
    if (!fse) {
2252
        /* We don't have a fsdev identified by fsdev_id */
2253
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2254
                    "with the id %s\n", conf->fsdev_id);
2255
        exit(1);
2256
    }
2257

    
2258
    if (!fse->path || !conf->tag) {
2259
        /* we haven't specified a mount_tag or the path */
2260
        fprintf(stderr, "fsdev with id %s needs path "
2261
                "and Virtio-9p device needs mount_tag arguments\n",
2262
                conf->fsdev_id);
2263
        exit(1);
2264
    }
2265

    
2266
    if (!strcmp(fse->security_model, "passthrough")) {
2267
        /* Files on the Fileserver set to client user credentials */
2268
        s->ctx.fs_sm = SM_PASSTHROUGH;
2269
    } else if (!strcmp(fse->security_model, "mapped")) {
2270
        /* Files on the fileserver are set to QEMU credentials.
2271
         * Client user credentials are saved in extended attributes.
2272
         */
2273
        s->ctx.fs_sm = SM_MAPPED;
2274
    } else {
2275
        /* user haven't specified a correct security option */
2276
        fprintf(stderr, "one of the following must be specified as the"
2277
                "security option:\n\t security_model=passthrough \n\t "
2278
                "security_model=mapped\n");
2279
        return NULL;
2280
    }
2281

    
2282
    if (lstat(fse->path, &stat)) {
2283
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2284
        exit(1);
2285
    } else if (!S_ISDIR(stat.st_mode)) {
2286
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2287
        exit(1);
2288
    }
2289

    
2290
    s->ctx.fs_root = qemu_strdup(fse->path);
2291
    len = strlen(conf->tag);
2292
    if (len > MAX_TAG_LEN) {
2293
        len = MAX_TAG_LEN;
2294
    }
2295
    /* s->tag is non-NULL terminated string */
2296
    s->tag = qemu_malloc(len);
2297
    memcpy(s->tag, conf->tag, len);
2298
    s->tag_len = len;
2299
    s->ctx.uid = -1;
2300

    
2301
    s->ops = fse->ops;
2302
    s->vdev.get_features = virtio_9p_get_features;
2303
    s->config_size = sizeof(struct virtio_9p_config) +
2304
                        s->tag_len;
2305
    s->vdev.get_config = virtio_9p_get_config;
2306

    
2307
    return &s->vdev;
2308
}