Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 00ec5c37

History | View | Annotate | Download (55.6 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, V9fsCreateState *vs)
174
{
175
    FsCred cred;
176

    
177
    cred_init(&cred);
178
    cred.fc_uid = vs->fidp->uid;
179
    cred.fc_mode = vs->perm & 0777;
180

    
181
    return s->ops->mkdir(&s->ctx, vs->fullname.data, &cred);
182
}
183

    
184
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
185
{
186
    return s->ops->fstat(&s->ctx, fd, stbuf);
187
}
188

    
189
static int v9fs_do_open2(V9fsState *s, V9fsCreateState *vs)
190
{
191
    FsCred cred;
192
    int flags;
193

    
194
    cred_init(&cred);
195
    cred.fc_uid = vs->fidp->uid;
196
    cred.fc_mode = vs->perm & 0777;
197
    flags = omode_to_uflags(vs->mode) | O_CREAT;
198

    
199
    return s->ops->open2(&s->ctx, vs->fullname.data, flags, &cred);
200
}
201

    
202
static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath,
203
                            V9fsString *newpath)
204
{
205
    return s->ops->symlink(&s->ctx, oldpath->data, newpath->data);
206
}
207

    
208
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
209
{
210
    return s->ops->link(&s->ctx, oldpath->data, newpath->data);
211
}
212

    
213
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
214
{
215
    return s->ops->truncate(&s->ctx, path->data, size);
216
}
217

    
218
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
219
                            V9fsString *newpath)
220
{
221
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
222
}
223

    
224
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
225
{
226
    FsCred cred;
227
    cred_init(&cred);
228
    cred.fc_uid = uid;
229
    cred.fc_gid = gid;
230

    
231
    return s->ops->chown(&s->ctx, path->data, &cred);
232
}
233

    
234
static int v9fs_do_utime(V9fsState *s, V9fsString *path,
235
                            const struct utimbuf *buf)
236
{
237
    return s->ops->utime(&s->ctx, path->data, buf);
238
}
239

    
240
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
241
{
242
    return s->ops->remove(&s->ctx, path->data);
243
}
244

    
245
static int v9fs_do_fsync(V9fsState *s, int fd)
246
{
247
    return s->ops->fsync(&s->ctx, fd);
248
}
249

    
250
static void v9fs_string_init(V9fsString *str)
251
{
252
    str->data = NULL;
253
    str->size = 0;
254
}
255

    
256
static void v9fs_string_free(V9fsString *str)
257
{
258
    qemu_free(str->data);
259
    str->data = NULL;
260
    str->size = 0;
261
}
262

    
263
static void v9fs_string_null(V9fsString *str)
264
{
265
    v9fs_string_free(str);
266
}
267

    
268
static int number_to_string(void *arg, char type)
269
{
270
    unsigned int ret = 0;
271

    
272
    switch (type) {
273
    case 'u': {
274
        unsigned int num = *(unsigned int *)arg;
275

    
276
        do {
277
            ret++;
278
            num = num/10;
279
        } while (num);
280
        break;
281
    }
282
    default:
283
        printf("Number_to_string: Unknown number format\n");
284
        return -1;
285
    }
286

    
287
    return ret;
288
}
289

    
290
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
291
{
292
    va_list ap2;
293
    char *iter = (char *)fmt;
294
    int len = 0;
295
    int nr_args = 0;
296
    char *arg_char_ptr;
297
    unsigned int arg_uint;
298

    
299
    /* Find the number of %'s that denotes an argument */
300
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
301
        nr_args++;
302
        iter++;
303
    }
304

    
305
    len = strlen(fmt) - 2*nr_args;
306

    
307
    if (!nr_args) {
308
        goto alloc_print;
309
    }
310

    
311
    va_copy(ap2, ap);
312

    
313
    iter = (char *)fmt;
314

    
315
    /* Now parse the format string */
316
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
317
        iter++;
318
        switch (*iter) {
319
        case 'u':
320
            arg_uint = va_arg(ap2, unsigned int);
321
            len += number_to_string((void *)&arg_uint, 'u');
322
            break;
323
        case 's':
324
            arg_char_ptr = va_arg(ap2, char *);
325
            len += strlen(arg_char_ptr);
326
            break;
327
        case 'c':
328
            len += 1;
329
            break;
330
        default:
331
            fprintf(stderr,
332
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
333
            return -1;
334
        }
335
        iter++;
336
    }
337

    
338
alloc_print:
339
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
340

    
341
    return vsprintf(*strp, fmt, ap);
342
}
343

    
344
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
345
{
346
    va_list ap;
347
    int err;
348

    
349
    v9fs_string_free(str);
350

    
351
    va_start(ap, fmt);
352
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
353
    BUG_ON(err == -1);
354
    va_end(ap);
355

    
356
    str->size = err;
357
}
358

    
359
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
360
{
361
    v9fs_string_free(lhs);
362
    v9fs_string_sprintf(lhs, "%s", rhs->data);
363
}
364

    
365
static size_t v9fs_string_size(V9fsString *str)
366
{
367
    return str->size;
368
}
369

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

    
374
    for (f = s->fid_list; f; f = f->next) {
375
        if (f->fid == fid) {
376
            return f;
377
        }
378
    }
379

    
380
    return NULL;
381
}
382

    
383
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
384
{
385
    V9fsFidState *f;
386

    
387
    f = lookup_fid(s, fid);
388
    if (f) {
389
        return NULL;
390
    }
391

    
392
    f = qemu_mallocz(sizeof(V9fsFidState));
393

    
394
    f->fid = fid;
395
    f->fd = -1;
396
    f->dir = NULL;
397

    
398
    f->next = s->fid_list;
399
    s->fid_list = f;
400

    
401
    return f;
402
}
403

    
404
static int free_fid(V9fsState *s, int32_t fid)
405
{
406
    V9fsFidState **fidpp, *fidp;
407

    
408
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
409
        if ((*fidpp)->fid == fid) {
410
            break;
411
        }
412
    }
413

    
414
    if (*fidpp == NULL) {
415
        return -ENOENT;
416
    }
417

    
418
    fidp = *fidpp;
419
    *fidpp = fidp->next;
420

    
421
    if (fidp->fd != -1) {
422
        v9fs_do_close(s, fidp->fd);
423
    }
424
    if (fidp->dir) {
425
        v9fs_do_closedir(s, fidp->dir);
426
    }
427
    v9fs_string_free(&fidp->path);
428
    qemu_free(fidp);
429

    
430
    return 0;
431
}
432

    
433
#define P9_QID_TYPE_DIR         0x80
434
#define P9_QID_TYPE_SYMLINK     0x02
435

    
436
#define P9_STAT_MODE_DIR        0x80000000
437
#define P9_STAT_MODE_APPEND     0x40000000
438
#define P9_STAT_MODE_EXCL       0x20000000
439
#define P9_STAT_MODE_MOUNT      0x10000000
440
#define P9_STAT_MODE_AUTH       0x08000000
441
#define P9_STAT_MODE_TMP        0x04000000
442
#define P9_STAT_MODE_SYMLINK    0x02000000
443
#define P9_STAT_MODE_LINK       0x01000000
444
#define P9_STAT_MODE_DEVICE     0x00800000
445
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
446
#define P9_STAT_MODE_SOCKET     0x00100000
447
#define P9_STAT_MODE_SETUID     0x00080000
448
#define P9_STAT_MODE_SETGID     0x00040000
449
#define P9_STAT_MODE_SETVTX     0x00010000
450

    
451
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
452
                                P9_STAT_MODE_SYMLINK |      \
453
                                P9_STAT_MODE_LINK |         \
454
                                P9_STAT_MODE_DEVICE |       \
455
                                P9_STAT_MODE_NAMED_PIPE |   \
456
                                P9_STAT_MODE_SOCKET)
457

    
458
/* This is the algorithm from ufs in spfs */
459
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
460
{
461
    size_t size;
462

    
463
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
464
    memcpy(&qidp->path, &stbuf->st_ino, size);
465
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
466
    qidp->type = 0;
467
    if (S_ISDIR(stbuf->st_mode)) {
468
        qidp->type |= P9_QID_TYPE_DIR;
469
    }
470
    if (S_ISLNK(stbuf->st_mode)) {
471
        qidp->type |= P9_QID_TYPE_SYMLINK;
472
    }
473
}
474

    
475
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
476
{
477
    struct stat stbuf;
478
    int err;
479

    
480
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
481
    if (err) {
482
        return err;
483
    }
484

    
485
    stat_to_qid(&stbuf, qidp);
486
    return 0;
487
}
488

    
489
static V9fsPDU *alloc_pdu(V9fsState *s)
490
{
491
    V9fsPDU *pdu = NULL;
492

    
493
    if (!QLIST_EMPTY(&s->free_list)) {
494
        pdu = QLIST_FIRST(&s->free_list);
495
        QLIST_REMOVE(pdu, next);
496
    }
497
    return pdu;
498
}
499

    
500
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
501
{
502
    if (pdu) {
503
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
504
    }
505
}
506

    
507
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
508
                        size_t offset, size_t size, int pack)
509
{
510
    int i = 0;
511
    size_t copied = 0;
512

    
513
    for (i = 0; size && i < sg_count; i++) {
514
        size_t len;
515
        if (offset >= sg[i].iov_len) {
516
            /* skip this sg */
517
            offset -= sg[i].iov_len;
518
            continue;
519
        } else {
520
            len = MIN(sg[i].iov_len - offset, size);
521
            if (pack) {
522
                memcpy(sg[i].iov_base + offset, addr, len);
523
            } else {
524
                memcpy(addr, sg[i].iov_base + offset, len);
525
            }
526
            size -= len;
527
            copied += len;
528
            addr += len;
529
            if (size) {
530
                offset = 0;
531
                continue;
532
            }
533
        }
534
    }
535

    
536
    return copied;
537
}
538

    
539
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
540
{
541
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
542
                         offset, size, 0);
543
}
544

    
545
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
546
                        size_t size)
547
{
548
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
549
                             offset, size, 1);
550
}
551

    
552
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
553
{
554
    size_t pos = 0;
555
    int i, j;
556
    struct iovec *src_sg;
557
    unsigned int num;
558

    
559
    if (rx) {
560
        src_sg = pdu->elem.in_sg;
561
        num = pdu->elem.in_num;
562
    } else {
563
        src_sg = pdu->elem.out_sg;
564
        num = pdu->elem.out_num;
565
    }
566

    
567
    j = 0;
568
    for (i = 0; i < num; i++) {
569
        if (offset <= pos) {
570
            sg[j].iov_base = src_sg[i].iov_base;
571
            sg[j].iov_len = src_sg[i].iov_len;
572
            j++;
573
        } else if (offset < (src_sg[i].iov_len + pos)) {
574
            sg[j].iov_base = src_sg[i].iov_base;
575
            sg[j].iov_len = src_sg[i].iov_len;
576
            sg[j].iov_base += (offset - pos);
577
            sg[j].iov_len -= (offset - pos);
578
            j++;
579
        }
580
        pos += src_sg[i].iov_len;
581
    }
582

    
583
    return j;
584
}
585

    
586
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
587
{
588
    size_t old_offset = offset;
589
    va_list ap;
590
    int i;
591

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

    
662
    va_end(ap);
663

    
664
    return offset - old_offset;
665
}
666

    
667
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
668
{
669
    size_t old_offset = offset;
670
    va_list ap;
671
    int i;
672

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

    
734
    return offset - old_offset;
735
}
736

    
737
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
738
{
739
    int8_t id = pdu->id + 1; /* Response */
740

    
741
    if (len < 0) {
742
        V9fsString str;
743
        int err = -len;
744

    
745
        str.data = strerror(err);
746
        str.size = strlen(str.data);
747

    
748
        len = 7;
749
        len += pdu_marshal(pdu, len, "s", &str);
750
        if (dotu) {
751
            len += pdu_marshal(pdu, len, "d", err);
752
        }
753

    
754
        id = P9_RERROR;
755
    }
756

    
757
    /* fill out the header */
758
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
759

    
760
    /* keep these in sync */
761
    pdu->size = len;
762
    pdu->id = id;
763

    
764
    /* push onto queue and notify */
765
    virtqueue_push(s->vq, &pdu->elem, len);
766

    
767
    /* FIXME: we should batch these completions */
768
    virtio_notify(&s->vdev, s->vq);
769

    
770
    free_pdu(s, pdu);
771
}
772

    
773
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
774
{
775
    mode_t ret;
776

    
777
    ret = mode & 0777;
778
    if (mode & P9_STAT_MODE_DIR) {
779
        ret |= S_IFDIR;
780
    }
781

    
782
    if (dotu) {
783
        if (mode & P9_STAT_MODE_SYMLINK) {
784
            ret |= S_IFLNK;
785
        }
786
        if (mode & P9_STAT_MODE_SOCKET) {
787
            ret |= S_IFSOCK;
788
        }
789
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
790
            ret |= S_IFIFO;
791
        }
792
        if (mode & P9_STAT_MODE_DEVICE) {
793
            if (extension && extension->data[0] == 'c') {
794
                ret |= S_IFCHR;
795
            } else {
796
                ret |= S_IFBLK;
797
            }
798
        }
799
    }
800

    
801
    if (!(ret&~0777)) {
802
        ret |= S_IFREG;
803
    }
804

    
805
    if (mode & P9_STAT_MODE_SETUID) {
806
        ret |= S_ISUID;
807
    }
808
    if (mode & P9_STAT_MODE_SETGID) {
809
        ret |= S_ISGID;
810
    }
811
    if (mode & P9_STAT_MODE_SETVTX) {
812
        ret |= S_ISVTX;
813
    }
814

    
815
    return ret;
816
}
817

    
818
static int donttouch_stat(V9fsStat *stat)
819
{
820
    if (stat->type == -1 &&
821
        stat->dev == -1 &&
822
        stat->qid.type == -1 &&
823
        stat->qid.version == -1 &&
824
        stat->qid.path == -1 &&
825
        stat->mode == -1 &&
826
        stat->atime == -1 &&
827
        stat->mtime == -1 &&
828
        stat->length == -1 &&
829
        !stat->name.size &&
830
        !stat->uid.size &&
831
        !stat->gid.size &&
832
        !stat->muid.size &&
833
        stat->n_uid == -1 &&
834
        stat->n_gid == -1 &&
835
        stat->n_muid == -1) {
836
        return 1;
837
    }
838

    
839
    return 0;
840
}
841

    
842
static void v9fs_stat_free(V9fsStat *stat)
843
{
844
    v9fs_string_free(&stat->name);
845
    v9fs_string_free(&stat->uid);
846
    v9fs_string_free(&stat->gid);
847
    v9fs_string_free(&stat->muid);
848
    v9fs_string_free(&stat->extension);
849
}
850

    
851
static uint32_t stat_to_v9mode(const struct stat *stbuf)
852
{
853
    uint32_t mode;
854

    
855
    mode = stbuf->st_mode & 0777;
856
    if (S_ISDIR(stbuf->st_mode)) {
857
        mode |= P9_STAT_MODE_DIR;
858
    }
859

    
860
    if (dotu) {
861
        if (S_ISLNK(stbuf->st_mode)) {
862
            mode |= P9_STAT_MODE_SYMLINK;
863
        }
864

    
865
        if (S_ISSOCK(stbuf->st_mode)) {
866
            mode |= P9_STAT_MODE_SOCKET;
867
        }
868

    
869
        if (S_ISFIFO(stbuf->st_mode)) {
870
            mode |= P9_STAT_MODE_NAMED_PIPE;
871
        }
872

    
873
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
874
            mode |= P9_STAT_MODE_DEVICE;
875
        }
876

    
877
        if (stbuf->st_mode & S_ISUID) {
878
            mode |= P9_STAT_MODE_SETUID;
879
        }
880

    
881
        if (stbuf->st_mode & S_ISGID) {
882
            mode |= P9_STAT_MODE_SETGID;
883
        }
884

    
885
        if (stbuf->st_mode & S_ISVTX) {
886
            mode |= P9_STAT_MODE_SETVTX;
887
        }
888
    }
889

    
890
    return mode;
891
}
892

    
893
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
894
                            const struct stat *stbuf,
895
                            V9fsStat *v9stat)
896
{
897
    int err;
898
    const char *str;
899

    
900
    memset(v9stat, 0, sizeof(*v9stat));
901

    
902
    stat_to_qid(stbuf, &v9stat->qid);
903
    v9stat->mode = stat_to_v9mode(stbuf);
904
    v9stat->atime = stbuf->st_atime;
905
    v9stat->mtime = stbuf->st_mtime;
906
    v9stat->length = stbuf->st_size;
907

    
908
    v9fs_string_null(&v9stat->uid);
909
    v9fs_string_null(&v9stat->gid);
910
    v9fs_string_null(&v9stat->muid);
911

    
912
    if (dotu) {
913
        v9stat->n_uid = stbuf->st_uid;
914
        v9stat->n_gid = stbuf->st_gid;
915
        v9stat->n_muid = 0;
916

    
917
        v9fs_string_null(&v9stat->extension);
918

    
919
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
920
            err = v9fs_do_readlink(s, name, &v9stat->extension);
921
            if (err == -1) {
922
                err = -errno;
923
                return err;
924
            }
925
            v9stat->extension.data[err] = 0;
926
            v9stat->extension.size = err;
927
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
928
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
929
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
930
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
931
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
932
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
933
                    "HARDLINKCOUNT", stbuf->st_nlink);
934
        }
935
    }
936

    
937
    str = strrchr(name->data, '/');
938
    if (str) {
939
        str += 1;
940
    } else {
941
        str = name->data;
942
    }
943

    
944
    v9fs_string_sprintf(&v9stat->name, "%s", str);
945

    
946
    v9stat->size = 61 +
947
        v9fs_string_size(&v9stat->name) +
948
        v9fs_string_size(&v9stat->uid) +
949
        v9fs_string_size(&v9stat->gid) +
950
        v9fs_string_size(&v9stat->muid) +
951
        v9fs_string_size(&v9stat->extension);
952
    return 0;
953
}
954

    
955
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
956
{
957
    while (len && *iovcnt) {
958
        if (len < sg->iov_len) {
959
            sg->iov_len -= len;
960
            sg->iov_base += len;
961
            len = 0;
962
        } else {
963
            len -= sg->iov_len;
964
            sg++;
965
            *iovcnt -= 1;
966
        }
967
    }
968

    
969
    return sg;
970
}
971

    
972
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
973
{
974
    int i;
975
    int total = 0;
976

    
977
    for (i = 0; i < *cnt; i++) {
978
        if ((total + sg[i].iov_len) > cap) {
979
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
980
            i++;
981
            break;
982
        }
983
        total += sg[i].iov_len;
984
    }
985

    
986
    *cnt = i;
987

    
988
    return sg;
989
}
990

    
991
static void print_sg(struct iovec *sg, int cnt)
992
{
993
    int i;
994

    
995
    printf("sg[%d]: {", cnt);
996
    for (i = 0; i < cnt; i++) {
997
        if (i) {
998
            printf(", ");
999
        }
1000
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1001
    }
1002
    printf("}\n");
1003
}
1004

    
1005
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1006
{
1007
    V9fsString str;
1008
    v9fs_string_init(&str);
1009
    v9fs_string_copy(&str, dst);
1010
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1011
    v9fs_string_free(&str);
1012
}
1013

    
1014
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1015
{
1016
    int32_t msize;
1017
    V9fsString version;
1018
    size_t offset = 7;
1019

    
1020
    pdu_unmarshal(pdu, offset, "ds", &msize, &version);
1021

    
1022
    if (strcmp(version.data, "9P2000.u")) {
1023
        v9fs_string_sprintf(&version, "unknown");
1024
    }
1025

    
1026
    offset += pdu_marshal(pdu, offset, "ds", msize, &version);
1027
    complete_pdu(s, pdu, offset);
1028

    
1029
    v9fs_string_free(&version);
1030
}
1031

    
1032
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1033
{
1034
    int32_t fid, afid, n_uname;
1035
    V9fsString uname, aname;
1036
    V9fsFidState *fidp;
1037
    V9fsQID qid;
1038
    size_t offset = 7;
1039
    ssize_t err;
1040

    
1041
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1042

    
1043
    fidp = alloc_fid(s, fid);
1044
    if (fidp == NULL) {
1045
        err = -EINVAL;
1046
        goto out;
1047
    }
1048

    
1049
    fidp->uid = n_uname;
1050

    
1051
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1052
    err = fid_to_qid(s, fidp, &qid);
1053
    if (err) {
1054
        err = -EINVAL;
1055
        free_fid(s, fid);
1056
        goto out;
1057
    }
1058

    
1059
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1060

    
1061
    err = offset;
1062
out:
1063
    complete_pdu(s, pdu, err);
1064
    v9fs_string_free(&uname);
1065
    v9fs_string_free(&aname);
1066
}
1067

    
1068
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1069
{
1070
    if (err == -1) {
1071
        err = -errno;
1072
        goto out;
1073
    }
1074

    
1075
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1076
    if (err) {
1077
        goto out;
1078
    }
1079
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1080
    err = vs->offset;
1081

    
1082
out:
1083
    complete_pdu(s, vs->pdu, err);
1084
    v9fs_stat_free(&vs->v9stat);
1085
    qemu_free(vs);
1086
}
1087

    
1088
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1089
{
1090
    int32_t fid;
1091
    V9fsStatState *vs;
1092
    ssize_t err = 0;
1093

    
1094
    vs = qemu_malloc(sizeof(*vs));
1095
    vs->pdu = pdu;
1096
    vs->offset = 7;
1097

    
1098
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1099

    
1100
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1101

    
1102
    vs->fidp = lookup_fid(s, fid);
1103
    if (vs->fidp == NULL) {
1104
        err = -ENOENT;
1105
        goto out;
1106
    }
1107

    
1108
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1109
    v9fs_stat_post_lstat(s, vs, err);
1110
    return;
1111

    
1112
out:
1113
    complete_pdu(s, vs->pdu, err);
1114
    v9fs_stat_free(&vs->v9stat);
1115
    qemu_free(vs);
1116
}
1117

    
1118
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1119
{
1120
    complete_pdu(s, vs->pdu, err);
1121

    
1122
    if (vs->nwnames) {
1123
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1124
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1125
        }
1126

    
1127
        qemu_free(vs->wnames);
1128
        qemu_free(vs->qids);
1129
    }
1130
}
1131

    
1132
static void v9fs_walk_marshal(V9fsWalkState *vs)
1133
{
1134
    int i;
1135
    vs->offset = 7;
1136
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1137

    
1138
    for (i = 0; i < vs->nwnames; i++) {
1139
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1140
    }
1141
}
1142

    
1143
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1144
                                                                int err)
1145
{
1146
    if (err == -1) {
1147
        free_fid(s, vs->newfidp->fid);
1148
        v9fs_string_free(&vs->path);
1149
        err = -ENOENT;
1150
        goto out;
1151
    }
1152

    
1153
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1154

    
1155
    vs->name_idx++;
1156
    if (vs->name_idx < vs->nwnames) {
1157
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1158
                                            vs->wnames[vs->name_idx].data);
1159
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1160

    
1161
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1162
        v9fs_walk_post_newfid_lstat(s, vs, err);
1163
        return;
1164
    }
1165

    
1166
    v9fs_string_free(&vs->path);
1167
    v9fs_walk_marshal(vs);
1168
    err = vs->offset;
1169
out:
1170
    v9fs_walk_complete(s, vs, err);
1171
}
1172

    
1173
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1174
        int err)
1175
{
1176
    if (err == -1) {
1177
        v9fs_string_free(&vs->path);
1178
        err = -ENOENT;
1179
        goto out;
1180
    }
1181

    
1182
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1183
    vs->name_idx++;
1184
    if (vs->name_idx < vs->nwnames) {
1185

    
1186
        v9fs_string_sprintf(&vs->path, "%s/%s",
1187
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1188
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1189

    
1190
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1191
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1192
        return;
1193
    }
1194

    
1195
    v9fs_string_free(&vs->path);
1196
    v9fs_walk_marshal(vs);
1197
    err = vs->offset;
1198
out:
1199
    v9fs_walk_complete(s, vs, err);
1200
}
1201

    
1202
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1203
{
1204
    int32_t fid, newfid;
1205
    V9fsWalkState *vs;
1206
    int err = 0;
1207
    int i;
1208

    
1209
    vs = qemu_malloc(sizeof(*vs));
1210
    vs->pdu = pdu;
1211
    vs->wnames = NULL;
1212
    vs->qids = NULL;
1213
    vs->offset = 7;
1214

    
1215
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1216
                                            &newfid, &vs->nwnames);
1217

    
1218
    if (vs->nwnames) {
1219
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1220

    
1221
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1222

    
1223
        for (i = 0; i < vs->nwnames; i++) {
1224
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1225
                                            &vs->wnames[i]);
1226
        }
1227
    }
1228

    
1229
    vs->fidp = lookup_fid(s, fid);
1230
    if (vs->fidp == NULL) {
1231
        err = -ENOENT;
1232
        goto out;
1233
    }
1234

    
1235
    /* FIXME: is this really valid? */
1236
    if (fid == newfid) {
1237

    
1238
        BUG_ON(vs->fidp->fd != -1);
1239
        BUG_ON(vs->fidp->dir);
1240
        v9fs_string_init(&vs->path);
1241
        vs->name_idx = 0;
1242

    
1243
        if (vs->name_idx < vs->nwnames) {
1244
            v9fs_string_sprintf(&vs->path, "%s/%s",
1245
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1246
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1247

    
1248
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1249
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1250
            return;
1251
        }
1252
    } else {
1253
        vs->newfidp = alloc_fid(s, newfid);
1254
        if (vs->newfidp == NULL) {
1255
            err = -EINVAL;
1256
            goto out;
1257
        }
1258

    
1259
        vs->newfidp->uid = vs->fidp->uid;
1260
        v9fs_string_init(&vs->path);
1261
        vs->name_idx = 0;
1262
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1263

    
1264
        if (vs->name_idx < vs->nwnames) {
1265
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1266
                                vs->wnames[vs->name_idx].data);
1267
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1268

    
1269
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1270
            v9fs_walk_post_newfid_lstat(s, vs, err);
1271
            return;
1272
        }
1273
    }
1274

    
1275
    v9fs_walk_marshal(vs);
1276
    err = vs->offset;
1277
out:
1278
    v9fs_walk_complete(s, vs, err);
1279
}
1280

    
1281
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1282
{
1283
    if (vs->fidp->dir == NULL) {
1284
        err = -errno;
1285
        goto out;
1286
    }
1287

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

    
1294
}
1295

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

    
1303
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1304
    err = vs->offset;
1305
out:
1306
    complete_pdu(s, vs->pdu, err);
1307
    qemu_free(vs);
1308
}
1309

    
1310
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1311
{
1312
    if (err) {
1313
        err = -errno;
1314
        goto out;
1315
    }
1316

    
1317
    stat_to_qid(&vs->stbuf, &vs->qid);
1318

    
1319
    if (S_ISDIR(vs->stbuf.st_mode)) {
1320
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1321
        v9fs_open_post_opendir(s, vs, err);
1322
    } else {
1323
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1324
                                    omode_to_uflags(vs->mode));
1325
        v9fs_open_post_open(s, vs, err);
1326
    }
1327
    return;
1328
out:
1329
    complete_pdu(s, vs->pdu, err);
1330
    qemu_free(vs);
1331
}
1332

    
1333
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1334
{
1335
    int32_t fid;
1336
    V9fsOpenState *vs;
1337
    ssize_t err = 0;
1338

    
1339

    
1340
    vs = qemu_malloc(sizeof(*vs));
1341
    vs->pdu = pdu;
1342
    vs->offset = 7;
1343

    
1344
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1345

    
1346
    vs->fidp = lookup_fid(s, fid);
1347
    if (vs->fidp == NULL) {
1348
        err = -ENOENT;
1349
        goto out;
1350
    }
1351

    
1352
    BUG_ON(vs->fidp->fd != -1);
1353
    BUG_ON(vs->fidp->dir);
1354

    
1355
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1356

    
1357
    v9fs_open_post_lstat(s, vs, err);
1358
    return;
1359
out:
1360
    complete_pdu(s, pdu, err);
1361
    qemu_free(vs);
1362
}
1363

    
1364
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1365
{
1366
    int32_t fid;
1367
    size_t offset = 7;
1368
    int err;
1369

    
1370
    pdu_unmarshal(pdu, offset, "d", &fid);
1371

    
1372
    err = free_fid(s, fid);
1373
    if (err < 0) {
1374
        goto out;
1375
    }
1376

    
1377
    offset = 7;
1378
    err = offset;
1379
out:
1380
    complete_pdu(s, pdu, err);
1381
}
1382

    
1383
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1384

    
1385
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1386
{
1387
    if (err) {
1388
        goto out;
1389
    }
1390
    v9fs_stat_free(&vs->v9stat);
1391
    v9fs_string_free(&vs->name);
1392
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1393
    vs->offset += vs->count;
1394
    err = vs->offset;
1395
out:
1396
    complete_pdu(s, vs->pdu, err);
1397
    qemu_free(vs);
1398
    return;
1399
}
1400

    
1401
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1402
                                    ssize_t err)
1403
{
1404
    if (err) {
1405
        err = -errno;
1406
        goto out;
1407
    }
1408
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1409
    if (err) {
1410
        goto out;
1411
    }
1412

    
1413
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1414
                            &vs->v9stat);
1415
    if ((vs->len != (vs->v9stat.size + 2)) ||
1416
            ((vs->count + vs->len) > vs->max_count)) {
1417
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1418
        v9fs_read_post_seekdir(s, vs, err);
1419
        return;
1420
    }
1421
    vs->count += vs->len;
1422
    v9fs_stat_free(&vs->v9stat);
1423
    v9fs_string_free(&vs->name);
1424
    vs->dir_pos = vs->dent->d_off;
1425
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1426
    v9fs_read_post_readdir(s, vs, err);
1427
    return;
1428
out:
1429
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1430
    v9fs_read_post_seekdir(s, vs, err);
1431
    return;
1432

    
1433
}
1434

    
1435
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1436
{
1437
    if (vs->dent) {
1438
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1439
        v9fs_string_init(&vs->name);
1440
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1441
                            vs->dent->d_name);
1442
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1443
        v9fs_read_post_dir_lstat(s, vs, err);
1444
        return;
1445
    }
1446

    
1447
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1448
    vs->offset += vs->count;
1449
    err = vs->offset;
1450
    complete_pdu(s, vs->pdu, err);
1451
    qemu_free(vs);
1452
    return;
1453
}
1454

    
1455
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1456
{
1457
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1458
    v9fs_read_post_readdir(s, vs, err);
1459
    return;
1460
}
1461

    
1462
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1463
                                       ssize_t err)
1464
{
1465
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1466
    v9fs_read_post_telldir(s, vs, err);
1467
    return;
1468
}
1469

    
1470
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1471
{
1472
    if (err  < 0) {
1473
        /* IO error return the error */
1474
        err = -errno;
1475
        goto out;
1476
    }
1477
    vs->total += vs->len;
1478
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1479
    if (vs->total < vs->count && vs->len > 0) {
1480
        do {
1481
            if (0) {
1482
                print_sg(vs->sg, vs->cnt);
1483
            }
1484
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1485
        } while (vs->len == -1 && errno == EINTR);
1486
        if (vs->len == -1) {
1487
            err  = -errno;
1488
        }
1489
        v9fs_read_post_readv(s, vs, err);
1490
        return;
1491
    }
1492
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1493
    vs->offset += vs->count;
1494
    err = vs->offset;
1495

    
1496
out:
1497
    complete_pdu(s, vs->pdu, err);
1498
    qemu_free(vs);
1499
}
1500

    
1501
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1502
{
1503
    if (err == -1) {
1504
        err = -errno;
1505
        goto out;
1506
    }
1507
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1508

    
1509
    if (vs->total < vs->count) {
1510
        do {
1511
            if (0) {
1512
                print_sg(vs->sg, vs->cnt);
1513
            }
1514
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1515
        } while (vs->len == -1 && errno == EINTR);
1516
        if (vs->len == -1) {
1517
            err  = -errno;
1518
        }
1519
        v9fs_read_post_readv(s, vs, err);
1520
        return;
1521
    }
1522
out:
1523
    complete_pdu(s, vs->pdu, err);
1524
    qemu_free(vs);
1525
}
1526

    
1527
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1528
{
1529
    int32_t fid;
1530
    V9fsReadState *vs;
1531
    ssize_t err = 0;
1532

    
1533
    vs = qemu_malloc(sizeof(*vs));
1534
    vs->pdu = pdu;
1535
    vs->offset = 7;
1536
    vs->total = 0;
1537
    vs->len = 0;
1538
    vs->count = 0;
1539

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

    
1542
    vs->fidp = lookup_fid(s, fid);
1543
    if (vs->fidp == NULL) {
1544
        err = -EINVAL;
1545
        goto out;
1546
    }
1547

    
1548
    if (vs->fidp->dir) {
1549
        vs->max_count = vs->count;
1550
        vs->count = 0;
1551
        if (vs->off == 0) {
1552
            v9fs_do_rewinddir(s, vs->fidp->dir);
1553
        }
1554
        v9fs_read_post_rewinddir(s, vs, err);
1555
        return;
1556
    } else if (vs->fidp->fd != -1) {
1557
        vs->sg = vs->iov;
1558
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1559
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1560
        v9fs_read_post_lseek(s, vs, err);
1561
        return;
1562
    } else {
1563
        err = -EINVAL;
1564
    }
1565
out:
1566
    complete_pdu(s, pdu, err);
1567
    qemu_free(vs);
1568
}
1569

    
1570
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1571
                                   ssize_t err)
1572
{
1573
    if (err  < 0) {
1574
        /* IO error return the error */
1575
        err = -errno;
1576
        goto out;
1577
    }
1578
    vs->total += vs->len;
1579
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1580
    if (vs->total < vs->count && vs->len > 0) {
1581
        do {
1582
            if (0) {
1583
                print_sg(vs->sg, vs->cnt);
1584
            }
1585
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1586
        } while (vs->len == -1 && errno == EINTR);
1587
        if (vs->len == -1) {
1588
            err  = -errno;
1589
        }
1590
        v9fs_write_post_writev(s, vs, err);
1591
        return;
1592
    }
1593
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1594

    
1595
    err = vs->offset;
1596
out:
1597
    complete_pdu(s, vs->pdu, err);
1598
    qemu_free(vs);
1599
}
1600

    
1601
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1602
{
1603
    if (err == -1) {
1604
        err = -errno;
1605
        goto out;
1606
    }
1607
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1608

    
1609
    if (vs->total < vs->count) {
1610
        do {
1611
            if (0) {
1612
                print_sg(vs->sg, vs->cnt);
1613
            }
1614
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1615
        } while (vs->len == -1 && errno == EINTR);
1616
        if (vs->len == -1) {
1617
            err  = -errno;
1618
        }
1619
        v9fs_write_post_writev(s, vs, err);
1620
        return;
1621
    }
1622

    
1623
out:
1624
    complete_pdu(s, vs->pdu, err);
1625
    qemu_free(vs);
1626
}
1627

    
1628
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1629
{
1630
    int32_t fid;
1631
    V9fsWriteState *vs;
1632
    ssize_t err;
1633

    
1634
    vs = qemu_malloc(sizeof(*vs));
1635

    
1636
    vs->pdu = pdu;
1637
    vs->offset = 7;
1638
    vs->sg = vs->iov;
1639
    vs->total = 0;
1640
    vs->len = 0;
1641

    
1642
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1643
                    vs->sg, &vs->cnt);
1644

    
1645
    vs->fidp = lookup_fid(s, fid);
1646
    if (vs->fidp == NULL) {
1647
        err = -EINVAL;
1648
        goto out;
1649
    }
1650

    
1651
    if (vs->fidp->fd == -1) {
1652
        err = -EINVAL;
1653
        goto out;
1654
    }
1655

    
1656
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1657

    
1658
    v9fs_write_post_lseek(s, vs, err);
1659
    return;
1660

    
1661
out:
1662
    complete_pdu(s, vs->pdu, err);
1663
    qemu_free(vs);
1664
}
1665

    
1666
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1667
{
1668
    if (err == 0) {
1669
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1670
        stat_to_qid(&vs->stbuf, &vs->qid);
1671

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

    
1674
        err = vs->offset;
1675
    }
1676

    
1677
    complete_pdu(s, vs->pdu, err);
1678
    v9fs_string_free(&vs->name);
1679
    v9fs_string_free(&vs->extension);
1680
    v9fs_string_free(&vs->fullname);
1681
    qemu_free(vs);
1682
}
1683

    
1684
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1685
{
1686
    if (err) {
1687
        err = -errno;
1688
    }
1689
    v9fs_post_create(s, vs, err);
1690
}
1691

    
1692
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1693
                                                                    int err)
1694
{
1695
    if (!vs->fidp->dir) {
1696
        err = -errno;
1697
    }
1698
    v9fs_post_create(s, vs, err);
1699
}
1700

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

    
1709
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1710
    v9fs_create_post_opendir(s, vs, err);
1711
    return;
1712

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

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

    
1724
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1725
    v9fs_create_post_dir_lstat(s, vs, err);
1726
    return;
1727

    
1728
out:
1729
    v9fs_post_create(s, vs, err);
1730
}
1731

    
1732
static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1733
                                                                int err)
1734
{
1735
    if (err) {
1736
        err = -errno;
1737
        goto out;
1738
    }
1739

    
1740
    err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1741
    v9fs_create_post_perms(s, vs, err);
1742
    return;
1743

    
1744
out:
1745
    v9fs_post_create(s, vs, err);
1746
}
1747

    
1748
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1749
{
1750
    if (err) {
1751
        vs->fidp->fd = -1;
1752
        err = -errno;
1753
    }
1754

    
1755
    v9fs_post_create(s, vs, err);
1756
    return;
1757
}
1758

    
1759
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1760
{
1761
    if (vs->fidp->fd == -1) {
1762
        err = -errno;
1763
        goto out;
1764
    }
1765

    
1766
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1767
    v9fs_create_post_fstat(s, vs, err);
1768

    
1769
    return;
1770

    
1771
out:
1772
    v9fs_post_create(s, vs, err);
1773

    
1774
}
1775

    
1776
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1777
{
1778

    
1779
    if (err == 0 || errno != ENOENT) {
1780
        err = -errno;
1781
        goto out;
1782
    }
1783

    
1784
    if (vs->perm & P9_STAT_MODE_DIR) {
1785
        err = v9fs_do_mkdir(s, vs);
1786
        v9fs_create_post_mkdir(s, vs, err);
1787
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
1788
        err = v9fs_do_symlink(s, &vs->extension, &vs->fullname);
1789
        v9fs_create_post_perms(s, vs, err);
1790
    } else if (vs->perm & P9_STAT_MODE_LINK) {
1791
        int32_t nfid = atoi(vs->extension.data);
1792
        V9fsFidState *nfidp = lookup_fid(s, nfid);
1793
        if (nfidp == NULL) {
1794
            err = -errno;
1795
            v9fs_post_create(s, vs, err);
1796
        }
1797
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1798
        v9fs_create_post_perms(s, vs, err);
1799
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1800
        char ctype;
1801
        uint32_t major, minor;
1802
        mode_t nmode = 0;
1803

    
1804
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1805
                                        &minor) != 3) {
1806
            err = -errno;
1807
            v9fs_post_create(s, vs, err);
1808
        }
1809

    
1810
        switch (ctype) {
1811
        case 'c':
1812
            nmode = S_IFCHR;
1813
            break;
1814
        case 'b':
1815
            nmode = S_IFBLK;
1816
            break;
1817
        default:
1818
            err = -EIO;
1819
            v9fs_post_create(s, vs, err);
1820
        }
1821

    
1822
        nmode |= vs->perm & 0777;
1823
        err = v9fs_do_mknod(s, &vs->fullname, nmode, makedev(major, minor));
1824
        v9fs_create_post_perms(s, vs, err);
1825
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1826
        err = v9fs_do_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
1827
        v9fs_post_create(s, vs, err);
1828
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1829
        err = v9fs_do_mksock(s, &vs->fullname);
1830
        v9fs_create_post_mksock(s, vs, err);
1831
    } else {
1832
        vs->fidp->fd = v9fs_do_open2(s, vs);
1833
        v9fs_create_post_open2(s, vs, err);
1834
    }
1835

    
1836
    return;
1837

    
1838
out:
1839
    v9fs_post_create(s, vs, err);
1840
}
1841

    
1842
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1843
{
1844
    int32_t fid;
1845
    V9fsCreateState *vs;
1846
    int err = 0;
1847

    
1848
    vs = qemu_malloc(sizeof(*vs));
1849
    vs->pdu = pdu;
1850
    vs->offset = 7;
1851

    
1852
    v9fs_string_init(&vs->fullname);
1853

    
1854
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1855
                                &vs->perm, &vs->mode, &vs->extension);
1856

    
1857
    vs->fidp = lookup_fid(s, fid);
1858
    if (vs->fidp == NULL) {
1859
        err = -EINVAL;
1860
        goto out;
1861
    }
1862

    
1863
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1864
                                                        vs->name.data);
1865

    
1866
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1867
    v9fs_create_post_lstat(s, vs, err);
1868
    return;
1869

    
1870
out:
1871
    complete_pdu(s, vs->pdu, err);
1872
    v9fs_string_free(&vs->name);
1873
    v9fs_string_free(&vs->extension);
1874
    qemu_free(vs);
1875
}
1876

    
1877
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1878
{
1879
    /* A nop call with no return */
1880
    complete_pdu(s, pdu, 7);
1881
}
1882

    
1883
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1884
                                                                int err)
1885
{
1886
    /* For TREMOVE we need to clunk the fid even on failed remove */
1887
    err = free_fid(s, vs->fidp->fid);
1888
    if (err < 0) {
1889
        goto out;
1890
    }
1891

    
1892
    err = vs->offset;
1893
out:
1894
    complete_pdu(s, vs->pdu, err);
1895
    qemu_free(vs);
1896
}
1897

    
1898
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1899
{
1900
    int32_t fid;
1901
    V9fsRemoveState *vs;
1902
    int err = 0;
1903

    
1904
    vs = qemu_malloc(sizeof(*vs));
1905
    vs->pdu = pdu;
1906
    vs->offset = 7;
1907

    
1908
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1909

    
1910
    vs->fidp = lookup_fid(s, fid);
1911
    if (vs->fidp == NULL) {
1912
        err = -EINVAL;
1913
        goto out;
1914
    }
1915

    
1916
    err = v9fs_do_remove(s, &vs->fidp->path);
1917
    v9fs_remove_post_remove(s, vs, err);
1918
    return;
1919

    
1920
out:
1921
    complete_pdu(s, pdu, err);
1922
    qemu_free(vs);
1923
}
1924

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

    
1931
    err = vs->offset;
1932

    
1933
out:
1934
    v9fs_stat_free(&vs->v9stat);
1935
    complete_pdu(s, vs->pdu, err);
1936
    qemu_free(vs);
1937
}
1938

    
1939
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
1940
{
1941
    if (err < 0) {
1942
        goto out;
1943
    }
1944

    
1945
    if (vs->v9stat.name.size != 0) {
1946
        v9fs_string_free(&vs->nname);
1947
    }
1948

    
1949
    if (vs->v9stat.length != -1) {
1950
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
1951
            err = -errno;
1952
        }
1953
    }
1954
    v9fs_wstat_post_truncate(s, vs, err);
1955
    return;
1956

    
1957
out:
1958
    v9fs_stat_free(&vs->v9stat);
1959
    complete_pdu(s, vs->pdu, err);
1960
    qemu_free(vs);
1961
}
1962

    
1963
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
1964
{
1965
    V9fsFidState *fidp;
1966
    if (err < 0) {
1967
        goto out;
1968
    }
1969

    
1970
    if (vs->v9stat.name.size != 0) {
1971
        char *old_name, *new_name;
1972
        char *end;
1973

    
1974
        old_name = vs->fidp->path.data;
1975
        end = strrchr(old_name, '/');
1976
        if (end) {
1977
            end++;
1978
        } else {
1979
            end = old_name;
1980
        }
1981

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

    
1984
        memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
1985
        memcpy(new_name, old_name, end - old_name);
1986
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
1987
                vs->v9stat.name.size);
1988
        vs->nname.data = new_name;
1989
        vs->nname.size = strlen(new_name);
1990

    
1991
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
1992
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
1993
                err = -errno;
1994
            } else {
1995
                /*
1996
                 * Fixup fid's pointing to the old name to
1997
                 * start pointing to the new name
1998
                 */
1999
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2000

    
2001
                    if (vs->fidp == fidp) {
2002
                        /*
2003
                         * we replace name of this fid towards the end
2004
                         * so that our below strcmp will work
2005
                         */
2006
                        continue;
2007
                    }
2008
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
2009
                                 strlen(vs->fidp->path.data))) {
2010
                        /* replace the name */
2011
                        v9fs_fix_path(&fidp->path, &vs->nname,
2012
                                      strlen(vs->fidp->path.data));
2013
                    }
2014
                }
2015
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2016
            }
2017
        }
2018
    }
2019
    v9fs_wstat_post_rename(s, vs, err);
2020
    return;
2021

    
2022
out:
2023
    v9fs_stat_free(&vs->v9stat);
2024
    complete_pdu(s, vs->pdu, err);
2025
    qemu_free(vs);
2026
}
2027

    
2028
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2029
{
2030
    if (err < 0) {
2031
        goto out;
2032
    }
2033

    
2034
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2035
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2036
                    vs->v9stat.n_gid)) {
2037
            err = -errno;
2038
        }
2039
    }
2040
    v9fs_wstat_post_chown(s, vs, err);
2041
    return;
2042

    
2043
out:
2044
    v9fs_stat_free(&vs->v9stat);
2045
    complete_pdu(s, vs->pdu, err);
2046
    qemu_free(vs);
2047
}
2048

    
2049
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2050
{
2051
    if (err < 0) {
2052
        goto out;
2053
    }
2054

    
2055
    if (vs->v9stat.mtime != -1) {
2056
        struct utimbuf tb;
2057
        tb.actime = 0;
2058
        tb.modtime = vs->v9stat.mtime;
2059
        if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2060
            err = -errno;
2061
        }
2062
    }
2063

    
2064
    v9fs_wstat_post_utime(s, vs, err);
2065
    return;
2066

    
2067
out:
2068
    v9fs_stat_free(&vs->v9stat);
2069
    complete_pdu(s, vs->pdu, err);
2070
    qemu_free(vs);
2071
}
2072

    
2073
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2074
{
2075
    if (err == -1) {
2076
        err = -errno;
2077
    }
2078
    v9fs_stat_free(&vs->v9stat);
2079
    complete_pdu(s, vs->pdu, err);
2080
    qemu_free(vs);
2081
}
2082

    
2083
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2084
{
2085
    uint32_t v9_mode;
2086

    
2087
    if (err == -1) {
2088
        err = -errno;
2089
        goto out;
2090
    }
2091

    
2092
    v9_mode = stat_to_v9mode(&vs->stbuf);
2093

    
2094
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2095
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2096
            /* Attempting to change the type */
2097
            err = -EIO;
2098
            goto out;
2099
    }
2100

    
2101
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2102
                    &vs->v9stat.extension))) {
2103
            err = -errno;
2104
     }
2105
    v9fs_wstat_post_chmod(s, vs, err);
2106
    return;
2107

    
2108
out:
2109
    v9fs_stat_free(&vs->v9stat);
2110
    complete_pdu(s, vs->pdu, err);
2111
    qemu_free(vs);
2112
}
2113

    
2114
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2115
{
2116
    int32_t fid;
2117
    V9fsWstatState *vs;
2118
    int err = 0;
2119

    
2120
    vs = qemu_malloc(sizeof(*vs));
2121
    vs->pdu = pdu;
2122
    vs->offset = 7;
2123

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

    
2126
    vs->fidp = lookup_fid(s, fid);
2127
    if (vs->fidp == NULL) {
2128
        err = -EINVAL;
2129
        goto out;
2130
    }
2131

    
2132
    /* do we need to sync the file? */
2133
    if (donttouch_stat(&vs->v9stat)) {
2134
        err = v9fs_do_fsync(s, vs->fidp->fd);
2135
        v9fs_wstat_post_fsync(s, vs, err);
2136
        return;
2137
    }
2138

    
2139
    if (vs->v9stat.mode != -1) {
2140
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2141
        v9fs_wstat_post_lstat(s, vs, err);
2142
        return;
2143
    }
2144

    
2145
    v9fs_wstat_post_chmod(s, vs, err);
2146
    return;
2147

    
2148
out:
2149
    v9fs_stat_free(&vs->v9stat);
2150
    complete_pdu(s, vs->pdu, err);
2151
    qemu_free(vs);
2152
}
2153

    
2154
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2155

    
2156
static pdu_handler_t *pdu_handlers[] = {
2157
    [P9_TVERSION] = v9fs_version,
2158
    [P9_TATTACH] = v9fs_attach,
2159
    [P9_TSTAT] = v9fs_stat,
2160
    [P9_TWALK] = v9fs_walk,
2161
    [P9_TCLUNK] = v9fs_clunk,
2162
    [P9_TOPEN] = v9fs_open,
2163
    [P9_TREAD] = v9fs_read,
2164
#if 0
2165
    [P9_TAUTH] = v9fs_auth,
2166
#endif
2167
    [P9_TFLUSH] = v9fs_flush,
2168
    [P9_TCREATE] = v9fs_create,
2169
    [P9_TWRITE] = v9fs_write,
2170
    [P9_TWSTAT] = v9fs_wstat,
2171
    [P9_TREMOVE] = v9fs_remove,
2172
};
2173

    
2174
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2175
{
2176
    pdu_handler_t *handler;
2177

    
2178
    if (debug_9p_pdu) {
2179
        pprint_pdu(pdu);
2180
    }
2181

    
2182
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2183

    
2184
    handler = pdu_handlers[pdu->id];
2185
    BUG_ON(handler == NULL);
2186

    
2187
    handler(s, pdu);
2188
}
2189

    
2190
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2191
{
2192
    V9fsState *s = (V9fsState *)vdev;
2193
    V9fsPDU *pdu;
2194
    ssize_t len;
2195

    
2196
    while ((pdu = alloc_pdu(s)) &&
2197
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2198
        uint8_t *ptr;
2199

    
2200
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2201
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2202

    
2203
        ptr = pdu->elem.out_sg[0].iov_base;
2204

    
2205
        memcpy(&pdu->size, ptr, 4);
2206
        pdu->id = ptr[4];
2207
        memcpy(&pdu->tag, ptr + 5, 2);
2208

    
2209
        submit_pdu(s, pdu);
2210
    }
2211

    
2212
    free_pdu(s, pdu);
2213
}
2214

    
2215
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2216
{
2217
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2218
    return features;
2219
}
2220

    
2221
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2222
{
2223
    return (V9fsState *)vdev;
2224
}
2225

    
2226
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2227
{
2228
    struct virtio_9p_config *cfg;
2229
    V9fsState *s = to_virtio_9p(vdev);
2230

    
2231
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2232
                        s->tag_len);
2233
    stw_raw(&cfg->tag_len, s->tag_len);
2234
    memcpy(cfg->tag, s->tag, s->tag_len);
2235
    memcpy(config, cfg, s->config_size);
2236
    qemu_free(cfg);
2237
}
2238

    
2239
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2240
 {
2241
    V9fsState *s;
2242
    int i, len;
2243
    struct stat stat;
2244
    FsTypeEntry *fse;
2245

    
2246

    
2247
    s = (V9fsState *)virtio_common_init("virtio-9p",
2248
                                    VIRTIO_ID_9P,
2249
                                    sizeof(struct virtio_9p_config)+
2250
                                    MAX_TAG_LEN,
2251
                                    sizeof(V9fsState));
2252

    
2253
    /* initialize pdu allocator */
2254
    QLIST_INIT(&s->free_list);
2255
    for (i = 0; i < (MAX_REQ - 1); i++) {
2256
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2257
    }
2258

    
2259
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2260

    
2261
    fse = get_fsdev_fsentry(conf->fsdev_id);
2262

    
2263
    if (!fse) {
2264
        /* We don't have a fsdev identified by fsdev_id */
2265
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2266
                    "with the id %s\n", conf->fsdev_id);
2267
        exit(1);
2268
    }
2269

    
2270
    if (!fse->path || !conf->tag) {
2271
        /* we haven't specified a mount_tag or the path */
2272
        fprintf(stderr, "fsdev with id %s needs path "
2273
                "and Virtio-9p device needs mount_tag arguments\n",
2274
                conf->fsdev_id);
2275
        exit(1);
2276
    }
2277

    
2278
    if (!strcmp(fse->security_model, "passthrough")) {
2279
        /* Files on the Fileserver set to client user credentials */
2280
        s->ctx.fs_sm = SM_PASSTHROUGH;
2281
    } else if (!strcmp(fse->security_model, "mapped")) {
2282
        /* Files on the fileserver are set to QEMU credentials.
2283
         * Client user credentials are saved in extended attributes.
2284
         */
2285
        s->ctx.fs_sm = SM_MAPPED;
2286
    } else {
2287
        /* user haven't specified a correct security option */
2288
        fprintf(stderr, "one of the following must be specified as the"
2289
                "security option:\n\t security_model=passthrough \n\t "
2290
                "security_model=mapped\n");
2291
        return NULL;
2292
    }
2293

    
2294
    if (lstat(fse->path, &stat)) {
2295
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2296
        exit(1);
2297
    } else if (!S_ISDIR(stat.st_mode)) {
2298
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2299
        exit(1);
2300
    }
2301

    
2302
    s->ctx.fs_root = qemu_strdup(fse->path);
2303
    len = strlen(conf->tag);
2304
    if (len > MAX_TAG_LEN) {
2305
        len = MAX_TAG_LEN;
2306
    }
2307
    /* s->tag is non-NULL terminated string */
2308
    s->tag = qemu_malloc(len);
2309
    memcpy(s->tag, conf->tag, len);
2310
    s->tag_len = len;
2311
    s->ctx.uid = -1;
2312

    
2313
    s->ops = fse->ops;
2314
    s->vdev.get_features = virtio_9p_get_features;
2315
    s->config_size = sizeof(struct virtio_9p_config) +
2316
                        s->tag_len;
2317
    s->vdev.get_config = virtio_9p_get_config;
2318

    
2319
    return &s->vdev;
2320
}