Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 1c293312

History | View | Annotate | Download (55.8 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, V9fsCreateState *vs, mode_t mode,
164
        dev_t dev)
165
{
166
    FsCred cred;
167
    cred_init(&cred);
168
    cred.fc_uid = vs->fidp->uid;
169
    cred.fc_mode = mode;
170
    cred.fc_rdev = dev;
171
    return s->ops->mknod(&s->ctx, vs->fullname.data, &cred);
172
}
173

    
174
static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
175
{
176
    return s->ops->mksock(&s->ctx, path->data);
177
}
178

    
179
static int v9fs_do_mkdir(V9fsState *s, V9fsCreateState *vs)
180
{
181
    FsCred cred;
182

    
183
    cred_init(&cred);
184
    cred.fc_uid = vs->fidp->uid;
185
    cred.fc_mode = vs->perm & 0777;
186

    
187
    return s->ops->mkdir(&s->ctx, vs->fullname.data, &cred);
188
}
189

    
190
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
191
{
192
    return s->ops->fstat(&s->ctx, fd, stbuf);
193
}
194

    
195
static int v9fs_do_open2(V9fsState *s, V9fsCreateState *vs)
196
{
197
    FsCred cred;
198
    int flags;
199

    
200
    cred_init(&cred);
201
    cred.fc_uid = vs->fidp->uid;
202
    cred.fc_mode = vs->perm & 0777;
203
    flags = omode_to_uflags(vs->mode) | O_CREAT;
204

    
205
    return s->ops->open2(&s->ctx, vs->fullname.data, flags, &cred);
206
}
207

    
208
static int v9fs_do_symlink(V9fsState *s, V9fsCreateState *vs)
209
{
210
    FsCred cred;
211
    cred_init(&cred);
212
    cred.fc_uid = vs->fidp->uid;
213
    cred.fc_mode = vs->perm | 0777;
214

    
215
    return s->ops->symlink(&s->ctx, vs->extension.data, vs->fullname.data,
216
            &cred);
217
}
218

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

    
224
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
225
{
226
    return s->ops->truncate(&s->ctx, path->data, size);
227
}
228

    
229
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
230
                            V9fsString *newpath)
231
{
232
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
233
}
234

    
235
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
236
{
237
    FsCred cred;
238
    cred_init(&cred);
239
    cred.fc_uid = uid;
240
    cred.fc_gid = gid;
241

    
242
    return s->ops->chown(&s->ctx, path->data, &cred);
243
}
244

    
245
static int v9fs_do_utime(V9fsState *s, V9fsString *path,
246
                            const struct utimbuf *buf)
247
{
248
    return s->ops->utime(&s->ctx, path->data, buf);
249
}
250

    
251
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
252
{
253
    return s->ops->remove(&s->ctx, path->data);
254
}
255

    
256
static int v9fs_do_fsync(V9fsState *s, int fd)
257
{
258
    return s->ops->fsync(&s->ctx, fd);
259
}
260

    
261
static void v9fs_string_init(V9fsString *str)
262
{
263
    str->data = NULL;
264
    str->size = 0;
265
}
266

    
267
static void v9fs_string_free(V9fsString *str)
268
{
269
    qemu_free(str->data);
270
    str->data = NULL;
271
    str->size = 0;
272
}
273

    
274
static void v9fs_string_null(V9fsString *str)
275
{
276
    v9fs_string_free(str);
277
}
278

    
279
static int number_to_string(void *arg, char type)
280
{
281
    unsigned int ret = 0;
282

    
283
    switch (type) {
284
    case 'u': {
285
        unsigned int num = *(unsigned int *)arg;
286

    
287
        do {
288
            ret++;
289
            num = num/10;
290
        } while (num);
291
        break;
292
    }
293
    default:
294
        printf("Number_to_string: Unknown number format\n");
295
        return -1;
296
    }
297

    
298
    return ret;
299
}
300

    
301
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
302
{
303
    va_list ap2;
304
    char *iter = (char *)fmt;
305
    int len = 0;
306
    int nr_args = 0;
307
    char *arg_char_ptr;
308
    unsigned int arg_uint;
309

    
310
    /* Find the number of %'s that denotes an argument */
311
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
312
        nr_args++;
313
        iter++;
314
    }
315

    
316
    len = strlen(fmt) - 2*nr_args;
317

    
318
    if (!nr_args) {
319
        goto alloc_print;
320
    }
321

    
322
    va_copy(ap2, ap);
323

    
324
    iter = (char *)fmt;
325

    
326
    /* Now parse the format string */
327
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
328
        iter++;
329
        switch (*iter) {
330
        case 'u':
331
            arg_uint = va_arg(ap2, unsigned int);
332
            len += number_to_string((void *)&arg_uint, 'u');
333
            break;
334
        case 's':
335
            arg_char_ptr = va_arg(ap2, char *);
336
            len += strlen(arg_char_ptr);
337
            break;
338
        case 'c':
339
            len += 1;
340
            break;
341
        default:
342
            fprintf(stderr,
343
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
344
            return -1;
345
        }
346
        iter++;
347
    }
348

    
349
alloc_print:
350
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
351

    
352
    return vsprintf(*strp, fmt, ap);
353
}
354

    
355
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
356
{
357
    va_list ap;
358
    int err;
359

    
360
    v9fs_string_free(str);
361

    
362
    va_start(ap, fmt);
363
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
364
    BUG_ON(err == -1);
365
    va_end(ap);
366

    
367
    str->size = err;
368
}
369

    
370
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
371
{
372
    v9fs_string_free(lhs);
373
    v9fs_string_sprintf(lhs, "%s", rhs->data);
374
}
375

    
376
static size_t v9fs_string_size(V9fsString *str)
377
{
378
    return str->size;
379
}
380

    
381
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
382
{
383
    V9fsFidState *f;
384

    
385
    for (f = s->fid_list; f; f = f->next) {
386
        if (f->fid == fid) {
387
            return f;
388
        }
389
    }
390

    
391
    return NULL;
392
}
393

    
394
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
395
{
396
    V9fsFidState *f;
397

    
398
    f = lookup_fid(s, fid);
399
    if (f) {
400
        return NULL;
401
    }
402

    
403
    f = qemu_mallocz(sizeof(V9fsFidState));
404

    
405
    f->fid = fid;
406
    f->fd = -1;
407
    f->dir = NULL;
408

    
409
    f->next = s->fid_list;
410
    s->fid_list = f;
411

    
412
    return f;
413
}
414

    
415
static int free_fid(V9fsState *s, int32_t fid)
416
{
417
    V9fsFidState **fidpp, *fidp;
418

    
419
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
420
        if ((*fidpp)->fid == fid) {
421
            break;
422
        }
423
    }
424

    
425
    if (*fidpp == NULL) {
426
        return -ENOENT;
427
    }
428

    
429
    fidp = *fidpp;
430
    *fidpp = fidp->next;
431

    
432
    if (fidp->fd != -1) {
433
        v9fs_do_close(s, fidp->fd);
434
    }
435
    if (fidp->dir) {
436
        v9fs_do_closedir(s, fidp->dir);
437
    }
438
    v9fs_string_free(&fidp->path);
439
    qemu_free(fidp);
440

    
441
    return 0;
442
}
443

    
444
#define P9_QID_TYPE_DIR         0x80
445
#define P9_QID_TYPE_SYMLINK     0x02
446

    
447
#define P9_STAT_MODE_DIR        0x80000000
448
#define P9_STAT_MODE_APPEND     0x40000000
449
#define P9_STAT_MODE_EXCL       0x20000000
450
#define P9_STAT_MODE_MOUNT      0x10000000
451
#define P9_STAT_MODE_AUTH       0x08000000
452
#define P9_STAT_MODE_TMP        0x04000000
453
#define P9_STAT_MODE_SYMLINK    0x02000000
454
#define P9_STAT_MODE_LINK       0x01000000
455
#define P9_STAT_MODE_DEVICE     0x00800000
456
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
457
#define P9_STAT_MODE_SOCKET     0x00100000
458
#define P9_STAT_MODE_SETUID     0x00080000
459
#define P9_STAT_MODE_SETGID     0x00040000
460
#define P9_STAT_MODE_SETVTX     0x00010000
461

    
462
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
463
                                P9_STAT_MODE_SYMLINK |      \
464
                                P9_STAT_MODE_LINK |         \
465
                                P9_STAT_MODE_DEVICE |       \
466
                                P9_STAT_MODE_NAMED_PIPE |   \
467
                                P9_STAT_MODE_SOCKET)
468

    
469
/* This is the algorithm from ufs in spfs */
470
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
471
{
472
    size_t size;
473

    
474
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
475
    memcpy(&qidp->path, &stbuf->st_ino, size);
476
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
477
    qidp->type = 0;
478
    if (S_ISDIR(stbuf->st_mode)) {
479
        qidp->type |= P9_QID_TYPE_DIR;
480
    }
481
    if (S_ISLNK(stbuf->st_mode)) {
482
        qidp->type |= P9_QID_TYPE_SYMLINK;
483
    }
484
}
485

    
486
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
487
{
488
    struct stat stbuf;
489
    int err;
490

    
491
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
492
    if (err) {
493
        return err;
494
    }
495

    
496
    stat_to_qid(&stbuf, qidp);
497
    return 0;
498
}
499

    
500
static V9fsPDU *alloc_pdu(V9fsState *s)
501
{
502
    V9fsPDU *pdu = NULL;
503

    
504
    if (!QLIST_EMPTY(&s->free_list)) {
505
        pdu = QLIST_FIRST(&s->free_list);
506
        QLIST_REMOVE(pdu, next);
507
    }
508
    return pdu;
509
}
510

    
511
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
512
{
513
    if (pdu) {
514
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
515
    }
516
}
517

    
518
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
519
                        size_t offset, size_t size, int pack)
520
{
521
    int i = 0;
522
    size_t copied = 0;
523

    
524
    for (i = 0; size && i < sg_count; i++) {
525
        size_t len;
526
        if (offset >= sg[i].iov_len) {
527
            /* skip this sg */
528
            offset -= sg[i].iov_len;
529
            continue;
530
        } else {
531
            len = MIN(sg[i].iov_len - offset, size);
532
            if (pack) {
533
                memcpy(sg[i].iov_base + offset, addr, len);
534
            } else {
535
                memcpy(addr, sg[i].iov_base + offset, len);
536
            }
537
            size -= len;
538
            copied += len;
539
            addr += len;
540
            if (size) {
541
                offset = 0;
542
                continue;
543
            }
544
        }
545
    }
546

    
547
    return copied;
548
}
549

    
550
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
551
{
552
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
553
                         offset, size, 0);
554
}
555

    
556
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
557
                        size_t size)
558
{
559
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
560
                             offset, size, 1);
561
}
562

    
563
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
564
{
565
    size_t pos = 0;
566
    int i, j;
567
    struct iovec *src_sg;
568
    unsigned int num;
569

    
570
    if (rx) {
571
        src_sg = pdu->elem.in_sg;
572
        num = pdu->elem.in_num;
573
    } else {
574
        src_sg = pdu->elem.out_sg;
575
        num = pdu->elem.out_num;
576
    }
577

    
578
    j = 0;
579
    for (i = 0; i < num; i++) {
580
        if (offset <= pos) {
581
            sg[j].iov_base = src_sg[i].iov_base;
582
            sg[j].iov_len = src_sg[i].iov_len;
583
            j++;
584
        } else if (offset < (src_sg[i].iov_len + pos)) {
585
            sg[j].iov_base = src_sg[i].iov_base;
586
            sg[j].iov_len = src_sg[i].iov_len;
587
            sg[j].iov_base += (offset - pos);
588
            sg[j].iov_len -= (offset - pos);
589
            j++;
590
        }
591
        pos += src_sg[i].iov_len;
592
    }
593

    
594
    return j;
595
}
596

    
597
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
598
{
599
    size_t old_offset = offset;
600
    va_list ap;
601
    int i;
602

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

    
673
    va_end(ap);
674

    
675
    return offset - old_offset;
676
}
677

    
678
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
679
{
680
    size_t old_offset = offset;
681
    va_list ap;
682
    int i;
683

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

    
745
    return offset - old_offset;
746
}
747

    
748
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
749
{
750
    int8_t id = pdu->id + 1; /* Response */
751

    
752
    if (len < 0) {
753
        V9fsString str;
754
        int err = -len;
755

    
756
        str.data = strerror(err);
757
        str.size = strlen(str.data);
758

    
759
        len = 7;
760
        len += pdu_marshal(pdu, len, "s", &str);
761
        if (dotu) {
762
            len += pdu_marshal(pdu, len, "d", err);
763
        }
764

    
765
        id = P9_RERROR;
766
    }
767

    
768
    /* fill out the header */
769
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
770

    
771
    /* keep these in sync */
772
    pdu->size = len;
773
    pdu->id = id;
774

    
775
    /* push onto queue and notify */
776
    virtqueue_push(s->vq, &pdu->elem, len);
777

    
778
    /* FIXME: we should batch these completions */
779
    virtio_notify(&s->vdev, s->vq);
780

    
781
    free_pdu(s, pdu);
782
}
783

    
784
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
785
{
786
    mode_t ret;
787

    
788
    ret = mode & 0777;
789
    if (mode & P9_STAT_MODE_DIR) {
790
        ret |= S_IFDIR;
791
    }
792

    
793
    if (dotu) {
794
        if (mode & P9_STAT_MODE_SYMLINK) {
795
            ret |= S_IFLNK;
796
        }
797
        if (mode & P9_STAT_MODE_SOCKET) {
798
            ret |= S_IFSOCK;
799
        }
800
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
801
            ret |= S_IFIFO;
802
        }
803
        if (mode & P9_STAT_MODE_DEVICE) {
804
            if (extension && extension->data[0] == 'c') {
805
                ret |= S_IFCHR;
806
            } else {
807
                ret |= S_IFBLK;
808
            }
809
        }
810
    }
811

    
812
    if (!(ret&~0777)) {
813
        ret |= S_IFREG;
814
    }
815

    
816
    if (mode & P9_STAT_MODE_SETUID) {
817
        ret |= S_ISUID;
818
    }
819
    if (mode & P9_STAT_MODE_SETGID) {
820
        ret |= S_ISGID;
821
    }
822
    if (mode & P9_STAT_MODE_SETVTX) {
823
        ret |= S_ISVTX;
824
    }
825

    
826
    return ret;
827
}
828

    
829
static int donttouch_stat(V9fsStat *stat)
830
{
831
    if (stat->type == -1 &&
832
        stat->dev == -1 &&
833
        stat->qid.type == -1 &&
834
        stat->qid.version == -1 &&
835
        stat->qid.path == -1 &&
836
        stat->mode == -1 &&
837
        stat->atime == -1 &&
838
        stat->mtime == -1 &&
839
        stat->length == -1 &&
840
        !stat->name.size &&
841
        !stat->uid.size &&
842
        !stat->gid.size &&
843
        !stat->muid.size &&
844
        stat->n_uid == -1 &&
845
        stat->n_gid == -1 &&
846
        stat->n_muid == -1) {
847
        return 1;
848
    }
849

    
850
    return 0;
851
}
852

    
853
static void v9fs_stat_free(V9fsStat *stat)
854
{
855
    v9fs_string_free(&stat->name);
856
    v9fs_string_free(&stat->uid);
857
    v9fs_string_free(&stat->gid);
858
    v9fs_string_free(&stat->muid);
859
    v9fs_string_free(&stat->extension);
860
}
861

    
862
static uint32_t stat_to_v9mode(const struct stat *stbuf)
863
{
864
    uint32_t mode;
865

    
866
    mode = stbuf->st_mode & 0777;
867
    if (S_ISDIR(stbuf->st_mode)) {
868
        mode |= P9_STAT_MODE_DIR;
869
    }
870

    
871
    if (dotu) {
872
        if (S_ISLNK(stbuf->st_mode)) {
873
            mode |= P9_STAT_MODE_SYMLINK;
874
        }
875

    
876
        if (S_ISSOCK(stbuf->st_mode)) {
877
            mode |= P9_STAT_MODE_SOCKET;
878
        }
879

    
880
        if (S_ISFIFO(stbuf->st_mode)) {
881
            mode |= P9_STAT_MODE_NAMED_PIPE;
882
        }
883

    
884
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
885
            mode |= P9_STAT_MODE_DEVICE;
886
        }
887

    
888
        if (stbuf->st_mode & S_ISUID) {
889
            mode |= P9_STAT_MODE_SETUID;
890
        }
891

    
892
        if (stbuf->st_mode & S_ISGID) {
893
            mode |= P9_STAT_MODE_SETGID;
894
        }
895

    
896
        if (stbuf->st_mode & S_ISVTX) {
897
            mode |= P9_STAT_MODE_SETVTX;
898
        }
899
    }
900

    
901
    return mode;
902
}
903

    
904
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
905
                            const struct stat *stbuf,
906
                            V9fsStat *v9stat)
907
{
908
    int err;
909
    const char *str;
910

    
911
    memset(v9stat, 0, sizeof(*v9stat));
912

    
913
    stat_to_qid(stbuf, &v9stat->qid);
914
    v9stat->mode = stat_to_v9mode(stbuf);
915
    v9stat->atime = stbuf->st_atime;
916
    v9stat->mtime = stbuf->st_mtime;
917
    v9stat->length = stbuf->st_size;
918

    
919
    v9fs_string_null(&v9stat->uid);
920
    v9fs_string_null(&v9stat->gid);
921
    v9fs_string_null(&v9stat->muid);
922

    
923
    if (dotu) {
924
        v9stat->n_uid = stbuf->st_uid;
925
        v9stat->n_gid = stbuf->st_gid;
926
        v9stat->n_muid = 0;
927

    
928
        v9fs_string_null(&v9stat->extension);
929

    
930
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
931
            err = v9fs_do_readlink(s, name, &v9stat->extension);
932
            if (err == -1) {
933
                err = -errno;
934
                return err;
935
            }
936
            v9stat->extension.data[err] = 0;
937
            v9stat->extension.size = err;
938
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
939
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
940
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
941
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
942
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
943
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
944
                    "HARDLINKCOUNT", stbuf->st_nlink);
945
        }
946
    }
947

    
948
    str = strrchr(name->data, '/');
949
    if (str) {
950
        str += 1;
951
    } else {
952
        str = name->data;
953
    }
954

    
955
    v9fs_string_sprintf(&v9stat->name, "%s", str);
956

    
957
    v9stat->size = 61 +
958
        v9fs_string_size(&v9stat->name) +
959
        v9fs_string_size(&v9stat->uid) +
960
        v9fs_string_size(&v9stat->gid) +
961
        v9fs_string_size(&v9stat->muid) +
962
        v9fs_string_size(&v9stat->extension);
963
    return 0;
964
}
965

    
966
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
967
{
968
    while (len && *iovcnt) {
969
        if (len < sg->iov_len) {
970
            sg->iov_len -= len;
971
            sg->iov_base += len;
972
            len = 0;
973
        } else {
974
            len -= sg->iov_len;
975
            sg++;
976
            *iovcnt -= 1;
977
        }
978
    }
979

    
980
    return sg;
981
}
982

    
983
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
984
{
985
    int i;
986
    int total = 0;
987

    
988
    for (i = 0; i < *cnt; i++) {
989
        if ((total + sg[i].iov_len) > cap) {
990
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
991
            i++;
992
            break;
993
        }
994
        total += sg[i].iov_len;
995
    }
996

    
997
    *cnt = i;
998

    
999
    return sg;
1000
}
1001

    
1002
static void print_sg(struct iovec *sg, int cnt)
1003
{
1004
    int i;
1005

    
1006
    printf("sg[%d]: {", cnt);
1007
    for (i = 0; i < cnt; i++) {
1008
        if (i) {
1009
            printf(", ");
1010
        }
1011
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1012
    }
1013
    printf("}\n");
1014
}
1015

    
1016
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1017
{
1018
    V9fsString str;
1019
    v9fs_string_init(&str);
1020
    v9fs_string_copy(&str, dst);
1021
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1022
    v9fs_string_free(&str);
1023
}
1024

    
1025
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1026
{
1027
    int32_t msize;
1028
    V9fsString version;
1029
    size_t offset = 7;
1030

    
1031
    pdu_unmarshal(pdu, offset, "ds", &msize, &version);
1032

    
1033
    if (strcmp(version.data, "9P2000.u")) {
1034
        v9fs_string_sprintf(&version, "unknown");
1035
    }
1036

    
1037
    offset += pdu_marshal(pdu, offset, "ds", msize, &version);
1038
    complete_pdu(s, pdu, offset);
1039

    
1040
    v9fs_string_free(&version);
1041
}
1042

    
1043
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1044
{
1045
    int32_t fid, afid, n_uname;
1046
    V9fsString uname, aname;
1047
    V9fsFidState *fidp;
1048
    V9fsQID qid;
1049
    size_t offset = 7;
1050
    ssize_t err;
1051

    
1052
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1053

    
1054
    fidp = alloc_fid(s, fid);
1055
    if (fidp == NULL) {
1056
        err = -EINVAL;
1057
        goto out;
1058
    }
1059

    
1060
    fidp->uid = n_uname;
1061

    
1062
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1063
    err = fid_to_qid(s, fidp, &qid);
1064
    if (err) {
1065
        err = -EINVAL;
1066
        free_fid(s, fid);
1067
        goto out;
1068
    }
1069

    
1070
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1071

    
1072
    err = offset;
1073
out:
1074
    complete_pdu(s, pdu, err);
1075
    v9fs_string_free(&uname);
1076
    v9fs_string_free(&aname);
1077
}
1078

    
1079
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1080
{
1081
    if (err == -1) {
1082
        err = -errno;
1083
        goto out;
1084
    }
1085

    
1086
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1087
    if (err) {
1088
        goto out;
1089
    }
1090
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1091
    err = vs->offset;
1092

    
1093
out:
1094
    complete_pdu(s, vs->pdu, err);
1095
    v9fs_stat_free(&vs->v9stat);
1096
    qemu_free(vs);
1097
}
1098

    
1099
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1100
{
1101
    int32_t fid;
1102
    V9fsStatState *vs;
1103
    ssize_t err = 0;
1104

    
1105
    vs = qemu_malloc(sizeof(*vs));
1106
    vs->pdu = pdu;
1107
    vs->offset = 7;
1108

    
1109
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1110

    
1111
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1112

    
1113
    vs->fidp = lookup_fid(s, fid);
1114
    if (vs->fidp == NULL) {
1115
        err = -ENOENT;
1116
        goto out;
1117
    }
1118

    
1119
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1120
    v9fs_stat_post_lstat(s, vs, err);
1121
    return;
1122

    
1123
out:
1124
    complete_pdu(s, vs->pdu, err);
1125
    v9fs_stat_free(&vs->v9stat);
1126
    qemu_free(vs);
1127
}
1128

    
1129
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1130
{
1131
    complete_pdu(s, vs->pdu, err);
1132

    
1133
    if (vs->nwnames) {
1134
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1135
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1136
        }
1137

    
1138
        qemu_free(vs->wnames);
1139
        qemu_free(vs->qids);
1140
    }
1141
}
1142

    
1143
static void v9fs_walk_marshal(V9fsWalkState *vs)
1144
{
1145
    int i;
1146
    vs->offset = 7;
1147
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1148

    
1149
    for (i = 0; i < vs->nwnames; i++) {
1150
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1151
    }
1152
}
1153

    
1154
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1155
                                                                int err)
1156
{
1157
    if (err == -1) {
1158
        free_fid(s, vs->newfidp->fid);
1159
        v9fs_string_free(&vs->path);
1160
        err = -ENOENT;
1161
        goto out;
1162
    }
1163

    
1164
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1165

    
1166
    vs->name_idx++;
1167
    if (vs->name_idx < vs->nwnames) {
1168
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1169
                                            vs->wnames[vs->name_idx].data);
1170
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1171

    
1172
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1173
        v9fs_walk_post_newfid_lstat(s, vs, err);
1174
        return;
1175
    }
1176

    
1177
    v9fs_string_free(&vs->path);
1178
    v9fs_walk_marshal(vs);
1179
    err = vs->offset;
1180
out:
1181
    v9fs_walk_complete(s, vs, err);
1182
}
1183

    
1184
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1185
        int err)
1186
{
1187
    if (err == -1) {
1188
        v9fs_string_free(&vs->path);
1189
        err = -ENOENT;
1190
        goto out;
1191
    }
1192

    
1193
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1194
    vs->name_idx++;
1195
    if (vs->name_idx < vs->nwnames) {
1196

    
1197
        v9fs_string_sprintf(&vs->path, "%s/%s",
1198
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1199
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1200

    
1201
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1202
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1203
        return;
1204
    }
1205

    
1206
    v9fs_string_free(&vs->path);
1207
    v9fs_walk_marshal(vs);
1208
    err = vs->offset;
1209
out:
1210
    v9fs_walk_complete(s, vs, err);
1211
}
1212

    
1213
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1214
{
1215
    int32_t fid, newfid;
1216
    V9fsWalkState *vs;
1217
    int err = 0;
1218
    int i;
1219

    
1220
    vs = qemu_malloc(sizeof(*vs));
1221
    vs->pdu = pdu;
1222
    vs->wnames = NULL;
1223
    vs->qids = NULL;
1224
    vs->offset = 7;
1225

    
1226
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1227
                                            &newfid, &vs->nwnames);
1228

    
1229
    if (vs->nwnames) {
1230
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1231

    
1232
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1233

    
1234
        for (i = 0; i < vs->nwnames; i++) {
1235
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1236
                                            &vs->wnames[i]);
1237
        }
1238
    }
1239

    
1240
    vs->fidp = lookup_fid(s, fid);
1241
    if (vs->fidp == NULL) {
1242
        err = -ENOENT;
1243
        goto out;
1244
    }
1245

    
1246
    /* FIXME: is this really valid? */
1247
    if (fid == newfid) {
1248

    
1249
        BUG_ON(vs->fidp->fd != -1);
1250
        BUG_ON(vs->fidp->dir);
1251
        v9fs_string_init(&vs->path);
1252
        vs->name_idx = 0;
1253

    
1254
        if (vs->name_idx < vs->nwnames) {
1255
            v9fs_string_sprintf(&vs->path, "%s/%s",
1256
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1257
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1258

    
1259
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1260
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1261
            return;
1262
        }
1263
    } else {
1264
        vs->newfidp = alloc_fid(s, newfid);
1265
        if (vs->newfidp == NULL) {
1266
            err = -EINVAL;
1267
            goto out;
1268
        }
1269

    
1270
        vs->newfidp->uid = vs->fidp->uid;
1271
        v9fs_string_init(&vs->path);
1272
        vs->name_idx = 0;
1273
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1274

    
1275
        if (vs->name_idx < vs->nwnames) {
1276
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1277
                                vs->wnames[vs->name_idx].data);
1278
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1279

    
1280
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1281
            v9fs_walk_post_newfid_lstat(s, vs, err);
1282
            return;
1283
        }
1284
    }
1285

    
1286
    v9fs_walk_marshal(vs);
1287
    err = vs->offset;
1288
out:
1289
    v9fs_walk_complete(s, vs, err);
1290
}
1291

    
1292
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1293
{
1294
    if (vs->fidp->dir == NULL) {
1295
        err = -errno;
1296
        goto out;
1297
    }
1298

    
1299
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1300
    err = vs->offset;
1301
out:
1302
    complete_pdu(s, vs->pdu, err);
1303
    qemu_free(vs);
1304

    
1305
}
1306

    
1307
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1308
{
1309
    if (vs->fidp->fd == -1) {
1310
        err = -errno;
1311
        goto out;
1312
    }
1313

    
1314
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1315
    err = vs->offset;
1316
out:
1317
    complete_pdu(s, vs->pdu, err);
1318
    qemu_free(vs);
1319
}
1320

    
1321
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1322
{
1323
    if (err) {
1324
        err = -errno;
1325
        goto out;
1326
    }
1327

    
1328
    stat_to_qid(&vs->stbuf, &vs->qid);
1329

    
1330
    if (S_ISDIR(vs->stbuf.st_mode)) {
1331
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1332
        v9fs_open_post_opendir(s, vs, err);
1333
    } else {
1334
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1335
                                    omode_to_uflags(vs->mode));
1336
        v9fs_open_post_open(s, vs, err);
1337
    }
1338
    return;
1339
out:
1340
    complete_pdu(s, vs->pdu, err);
1341
    qemu_free(vs);
1342
}
1343

    
1344
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1345
{
1346
    int32_t fid;
1347
    V9fsOpenState *vs;
1348
    ssize_t err = 0;
1349

    
1350

    
1351
    vs = qemu_malloc(sizeof(*vs));
1352
    vs->pdu = pdu;
1353
    vs->offset = 7;
1354

    
1355
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1356

    
1357
    vs->fidp = lookup_fid(s, fid);
1358
    if (vs->fidp == NULL) {
1359
        err = -ENOENT;
1360
        goto out;
1361
    }
1362

    
1363
    BUG_ON(vs->fidp->fd != -1);
1364
    BUG_ON(vs->fidp->dir);
1365

    
1366
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1367

    
1368
    v9fs_open_post_lstat(s, vs, err);
1369
    return;
1370
out:
1371
    complete_pdu(s, pdu, err);
1372
    qemu_free(vs);
1373
}
1374

    
1375
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1376
{
1377
    int32_t fid;
1378
    size_t offset = 7;
1379
    int err;
1380

    
1381
    pdu_unmarshal(pdu, offset, "d", &fid);
1382

    
1383
    err = free_fid(s, fid);
1384
    if (err < 0) {
1385
        goto out;
1386
    }
1387

    
1388
    offset = 7;
1389
    err = offset;
1390
out:
1391
    complete_pdu(s, pdu, err);
1392
}
1393

    
1394
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1395

    
1396
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1397
{
1398
    if (err) {
1399
        goto out;
1400
    }
1401
    v9fs_stat_free(&vs->v9stat);
1402
    v9fs_string_free(&vs->name);
1403
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1404
    vs->offset += vs->count;
1405
    err = vs->offset;
1406
out:
1407
    complete_pdu(s, vs->pdu, err);
1408
    qemu_free(vs);
1409
    return;
1410
}
1411

    
1412
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1413
                                    ssize_t err)
1414
{
1415
    if (err) {
1416
        err = -errno;
1417
        goto out;
1418
    }
1419
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1420
    if (err) {
1421
        goto out;
1422
    }
1423

    
1424
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1425
                            &vs->v9stat);
1426
    if ((vs->len != (vs->v9stat.size + 2)) ||
1427
            ((vs->count + vs->len) > vs->max_count)) {
1428
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1429
        v9fs_read_post_seekdir(s, vs, err);
1430
        return;
1431
    }
1432
    vs->count += vs->len;
1433
    v9fs_stat_free(&vs->v9stat);
1434
    v9fs_string_free(&vs->name);
1435
    vs->dir_pos = vs->dent->d_off;
1436
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1437
    v9fs_read_post_readdir(s, vs, err);
1438
    return;
1439
out:
1440
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1441
    v9fs_read_post_seekdir(s, vs, err);
1442
    return;
1443

    
1444
}
1445

    
1446
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1447
{
1448
    if (vs->dent) {
1449
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1450
        v9fs_string_init(&vs->name);
1451
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1452
                            vs->dent->d_name);
1453
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1454
        v9fs_read_post_dir_lstat(s, vs, err);
1455
        return;
1456
    }
1457

    
1458
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1459
    vs->offset += vs->count;
1460
    err = vs->offset;
1461
    complete_pdu(s, vs->pdu, err);
1462
    qemu_free(vs);
1463
    return;
1464
}
1465

    
1466
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1467
{
1468
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1469
    v9fs_read_post_readdir(s, vs, err);
1470
    return;
1471
}
1472

    
1473
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1474
                                       ssize_t err)
1475
{
1476
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1477
    v9fs_read_post_telldir(s, vs, err);
1478
    return;
1479
}
1480

    
1481
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1482
{
1483
    if (err  < 0) {
1484
        /* IO error return the error */
1485
        err = -errno;
1486
        goto out;
1487
    }
1488
    vs->total += vs->len;
1489
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1490
    if (vs->total < vs->count && vs->len > 0) {
1491
        do {
1492
            if (0) {
1493
                print_sg(vs->sg, vs->cnt);
1494
            }
1495
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1496
        } while (vs->len == -1 && errno == EINTR);
1497
        if (vs->len == -1) {
1498
            err  = -errno;
1499
        }
1500
        v9fs_read_post_readv(s, vs, err);
1501
        return;
1502
    }
1503
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1504
    vs->offset += vs->count;
1505
    err = vs->offset;
1506

    
1507
out:
1508
    complete_pdu(s, vs->pdu, err);
1509
    qemu_free(vs);
1510
}
1511

    
1512
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1513
{
1514
    if (err == -1) {
1515
        err = -errno;
1516
        goto out;
1517
    }
1518
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1519

    
1520
    if (vs->total < vs->count) {
1521
        do {
1522
            if (0) {
1523
                print_sg(vs->sg, vs->cnt);
1524
            }
1525
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1526
        } while (vs->len == -1 && errno == EINTR);
1527
        if (vs->len == -1) {
1528
            err  = -errno;
1529
        }
1530
        v9fs_read_post_readv(s, vs, err);
1531
        return;
1532
    }
1533
out:
1534
    complete_pdu(s, vs->pdu, err);
1535
    qemu_free(vs);
1536
}
1537

    
1538
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1539
{
1540
    int32_t fid;
1541
    V9fsReadState *vs;
1542
    ssize_t err = 0;
1543

    
1544
    vs = qemu_malloc(sizeof(*vs));
1545
    vs->pdu = pdu;
1546
    vs->offset = 7;
1547
    vs->total = 0;
1548
    vs->len = 0;
1549
    vs->count = 0;
1550

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

    
1553
    vs->fidp = lookup_fid(s, fid);
1554
    if (vs->fidp == NULL) {
1555
        err = -EINVAL;
1556
        goto out;
1557
    }
1558

    
1559
    if (vs->fidp->dir) {
1560
        vs->max_count = vs->count;
1561
        vs->count = 0;
1562
        if (vs->off == 0) {
1563
            v9fs_do_rewinddir(s, vs->fidp->dir);
1564
        }
1565
        v9fs_read_post_rewinddir(s, vs, err);
1566
        return;
1567
    } else if (vs->fidp->fd != -1) {
1568
        vs->sg = vs->iov;
1569
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1570
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1571
        v9fs_read_post_lseek(s, vs, err);
1572
        return;
1573
    } else {
1574
        err = -EINVAL;
1575
    }
1576
out:
1577
    complete_pdu(s, pdu, err);
1578
    qemu_free(vs);
1579
}
1580

    
1581
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1582
                                   ssize_t err)
1583
{
1584
    if (err  < 0) {
1585
        /* IO error return the error */
1586
        err = -errno;
1587
        goto out;
1588
    }
1589
    vs->total += vs->len;
1590
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1591
    if (vs->total < vs->count && vs->len > 0) {
1592
        do {
1593
            if (0) {
1594
                print_sg(vs->sg, vs->cnt);
1595
            }
1596
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1597
        } while (vs->len == -1 && errno == EINTR);
1598
        if (vs->len == -1) {
1599
            err  = -errno;
1600
        }
1601
        v9fs_write_post_writev(s, vs, err);
1602
        return;
1603
    }
1604
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1605

    
1606
    err = vs->offset;
1607
out:
1608
    complete_pdu(s, vs->pdu, err);
1609
    qemu_free(vs);
1610
}
1611

    
1612
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1613
{
1614
    if (err == -1) {
1615
        err = -errno;
1616
        goto out;
1617
    }
1618
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1619

    
1620
    if (vs->total < vs->count) {
1621
        do {
1622
            if (0) {
1623
                print_sg(vs->sg, vs->cnt);
1624
            }
1625
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1626
        } while (vs->len == -1 && errno == EINTR);
1627
        if (vs->len == -1) {
1628
            err  = -errno;
1629
        }
1630
        v9fs_write_post_writev(s, vs, err);
1631
        return;
1632
    }
1633

    
1634
out:
1635
    complete_pdu(s, vs->pdu, err);
1636
    qemu_free(vs);
1637
}
1638

    
1639
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1640
{
1641
    int32_t fid;
1642
    V9fsWriteState *vs;
1643
    ssize_t err;
1644

    
1645
    vs = qemu_malloc(sizeof(*vs));
1646

    
1647
    vs->pdu = pdu;
1648
    vs->offset = 7;
1649
    vs->sg = vs->iov;
1650
    vs->total = 0;
1651
    vs->len = 0;
1652

    
1653
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1654
                    vs->sg, &vs->cnt);
1655

    
1656
    vs->fidp = lookup_fid(s, fid);
1657
    if (vs->fidp == NULL) {
1658
        err = -EINVAL;
1659
        goto out;
1660
    }
1661

    
1662
    if (vs->fidp->fd == -1) {
1663
        err = -EINVAL;
1664
        goto out;
1665
    }
1666

    
1667
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1668

    
1669
    v9fs_write_post_lseek(s, vs, err);
1670
    return;
1671

    
1672
out:
1673
    complete_pdu(s, vs->pdu, err);
1674
    qemu_free(vs);
1675
}
1676

    
1677
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1678
{
1679
    if (err == 0) {
1680
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1681
        stat_to_qid(&vs->stbuf, &vs->qid);
1682

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

    
1685
        err = vs->offset;
1686
    }
1687

    
1688
    complete_pdu(s, vs->pdu, err);
1689
    v9fs_string_free(&vs->name);
1690
    v9fs_string_free(&vs->extension);
1691
    v9fs_string_free(&vs->fullname);
1692
    qemu_free(vs);
1693
}
1694

    
1695
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1696
{
1697
    if (err) {
1698
        err = -errno;
1699
    }
1700
    v9fs_post_create(s, vs, err);
1701
}
1702

    
1703
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1704
                                                                    int err)
1705
{
1706
    if (!vs->fidp->dir) {
1707
        err = -errno;
1708
    }
1709
    v9fs_post_create(s, vs, err);
1710
}
1711

    
1712
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1713
                                                                    int err)
1714
{
1715
    if (err) {
1716
        err = -errno;
1717
        goto out;
1718
    }
1719

    
1720
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1721
    v9fs_create_post_opendir(s, vs, err);
1722
    return;
1723

    
1724
out:
1725
    v9fs_post_create(s, vs, err);
1726
}
1727

    
1728
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1729
{
1730
    if (err) {
1731
        err = -errno;
1732
        goto out;
1733
    }
1734

    
1735
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1736
    v9fs_create_post_dir_lstat(s, vs, err);
1737
    return;
1738

    
1739
out:
1740
    v9fs_post_create(s, vs, err);
1741
}
1742

    
1743
static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1744
                                                                int err)
1745
{
1746
    if (err) {
1747
        err = -errno;
1748
        goto out;
1749
    }
1750

    
1751
    err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1752
    v9fs_create_post_perms(s, vs, err);
1753
    return;
1754

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

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

    
1766
    v9fs_post_create(s, vs, err);
1767
    return;
1768
}
1769

    
1770
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1771
{
1772
    if (vs->fidp->fd == -1) {
1773
        err = -errno;
1774
        goto out;
1775
    }
1776

    
1777
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1778
    v9fs_create_post_fstat(s, vs, err);
1779

    
1780
    return;
1781

    
1782
out:
1783
    v9fs_post_create(s, vs, err);
1784

    
1785
}
1786

    
1787
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1788
{
1789

    
1790
    if (err == 0 || errno != ENOENT) {
1791
        err = -errno;
1792
        goto out;
1793
    }
1794

    
1795
    if (vs->perm & P9_STAT_MODE_DIR) {
1796
        err = v9fs_do_mkdir(s, vs);
1797
        v9fs_create_post_mkdir(s, vs, err);
1798
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
1799
        err = v9fs_do_symlink(s, vs);
1800
        v9fs_create_post_perms(s, vs, err);
1801
    } else if (vs->perm & P9_STAT_MODE_LINK) {
1802
        int32_t nfid = atoi(vs->extension.data);
1803
        V9fsFidState *nfidp = lookup_fid(s, nfid);
1804
        if (nfidp == NULL) {
1805
            err = -errno;
1806
            v9fs_post_create(s, vs, err);
1807
        }
1808
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1809
        v9fs_create_post_perms(s, vs, err);
1810
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1811
        char ctype;
1812
        uint32_t major, minor;
1813
        mode_t nmode = 0;
1814

    
1815
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1816
                                        &minor) != 3) {
1817
            err = -errno;
1818
            v9fs_post_create(s, vs, err);
1819
        }
1820

    
1821
        switch (ctype) {
1822
        case 'c':
1823
            nmode = S_IFCHR;
1824
            break;
1825
        case 'b':
1826
            nmode = S_IFBLK;
1827
            break;
1828
        default:
1829
            err = -EIO;
1830
            v9fs_post_create(s, vs, err);
1831
        }
1832

    
1833
        nmode |= vs->perm & 0777;
1834
        err = v9fs_do_mknod(s, vs, nmode, makedev(major, minor));
1835
        v9fs_create_post_perms(s, vs, err);
1836
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1837
        err = v9fs_do_mknod(s, vs, S_IFIFO | (vs->perm & 0777), 0);
1838
        v9fs_post_create(s, vs, err);
1839
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1840
        err = v9fs_do_mksock(s, &vs->fullname);
1841
        v9fs_create_post_mksock(s, vs, err);
1842
    } else {
1843
        vs->fidp->fd = v9fs_do_open2(s, vs);
1844
        v9fs_create_post_open2(s, vs, err);
1845
    }
1846

    
1847
    return;
1848

    
1849
out:
1850
    v9fs_post_create(s, vs, err);
1851
}
1852

    
1853
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1854
{
1855
    int32_t fid;
1856
    V9fsCreateState *vs;
1857
    int err = 0;
1858

    
1859
    vs = qemu_malloc(sizeof(*vs));
1860
    vs->pdu = pdu;
1861
    vs->offset = 7;
1862

    
1863
    v9fs_string_init(&vs->fullname);
1864

    
1865
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1866
                                &vs->perm, &vs->mode, &vs->extension);
1867

    
1868
    vs->fidp = lookup_fid(s, fid);
1869
    if (vs->fidp == NULL) {
1870
        err = -EINVAL;
1871
        goto out;
1872
    }
1873

    
1874
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1875
                                                        vs->name.data);
1876

    
1877
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1878
    v9fs_create_post_lstat(s, vs, err);
1879
    return;
1880

    
1881
out:
1882
    complete_pdu(s, vs->pdu, err);
1883
    v9fs_string_free(&vs->name);
1884
    v9fs_string_free(&vs->extension);
1885
    qemu_free(vs);
1886
}
1887

    
1888
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1889
{
1890
    /* A nop call with no return */
1891
    complete_pdu(s, pdu, 7);
1892
}
1893

    
1894
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1895
                                                                int err)
1896
{
1897
    /* For TREMOVE we need to clunk the fid even on failed remove */
1898
    err = free_fid(s, vs->fidp->fid);
1899
    if (err < 0) {
1900
        goto out;
1901
    }
1902

    
1903
    err = vs->offset;
1904
out:
1905
    complete_pdu(s, vs->pdu, err);
1906
    qemu_free(vs);
1907
}
1908

    
1909
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1910
{
1911
    int32_t fid;
1912
    V9fsRemoveState *vs;
1913
    int err = 0;
1914

    
1915
    vs = qemu_malloc(sizeof(*vs));
1916
    vs->pdu = pdu;
1917
    vs->offset = 7;
1918

    
1919
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1920

    
1921
    vs->fidp = lookup_fid(s, fid);
1922
    if (vs->fidp == NULL) {
1923
        err = -EINVAL;
1924
        goto out;
1925
    }
1926

    
1927
    err = v9fs_do_remove(s, &vs->fidp->path);
1928
    v9fs_remove_post_remove(s, vs, err);
1929
    return;
1930

    
1931
out:
1932
    complete_pdu(s, pdu, err);
1933
    qemu_free(vs);
1934
}
1935

    
1936
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1937
{
1938
    if (err < 0) {
1939
        goto out;
1940
    }
1941

    
1942
    err = vs->offset;
1943

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

    
1950
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
1951
{
1952
    if (err < 0) {
1953
        goto out;
1954
    }
1955

    
1956
    if (vs->v9stat.name.size != 0) {
1957
        v9fs_string_free(&vs->nname);
1958
    }
1959

    
1960
    if (vs->v9stat.length != -1) {
1961
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
1962
            err = -errno;
1963
        }
1964
    }
1965
    v9fs_wstat_post_truncate(s, vs, err);
1966
    return;
1967

    
1968
out:
1969
    v9fs_stat_free(&vs->v9stat);
1970
    complete_pdu(s, vs->pdu, err);
1971
    qemu_free(vs);
1972
}
1973

    
1974
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
1975
{
1976
    V9fsFidState *fidp;
1977
    if (err < 0) {
1978
        goto out;
1979
    }
1980

    
1981
    if (vs->v9stat.name.size != 0) {
1982
        char *old_name, *new_name;
1983
        char *end;
1984

    
1985
        old_name = vs->fidp->path.data;
1986
        end = strrchr(old_name, '/');
1987
        if (end) {
1988
            end++;
1989
        } else {
1990
            end = old_name;
1991
        }
1992

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

    
1995
        memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
1996
        memcpy(new_name, old_name, end - old_name);
1997
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
1998
                vs->v9stat.name.size);
1999
        vs->nname.data = new_name;
2000
        vs->nname.size = strlen(new_name);
2001

    
2002
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
2003
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2004
                err = -errno;
2005
            } else {
2006
                /*
2007
                 * Fixup fid's pointing to the old name to
2008
                 * start pointing to the new name
2009
                 */
2010
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2011

    
2012
                    if (vs->fidp == fidp) {
2013
                        /*
2014
                         * we replace name of this fid towards the end
2015
                         * so that our below strcmp will work
2016
                         */
2017
                        continue;
2018
                    }
2019
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
2020
                                 strlen(vs->fidp->path.data))) {
2021
                        /* replace the name */
2022
                        v9fs_fix_path(&fidp->path, &vs->nname,
2023
                                      strlen(vs->fidp->path.data));
2024
                    }
2025
                }
2026
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2027
            }
2028
        }
2029
    }
2030
    v9fs_wstat_post_rename(s, vs, err);
2031
    return;
2032

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

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

    
2045
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2046
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2047
                    vs->v9stat.n_gid)) {
2048
            err = -errno;
2049
        }
2050
    }
2051
    v9fs_wstat_post_chown(s, vs, err);
2052
    return;
2053

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

    
2060
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2061
{
2062
    if (err < 0) {
2063
        goto out;
2064
    }
2065

    
2066
    if (vs->v9stat.mtime != -1) {
2067
        struct utimbuf tb;
2068
        tb.actime = 0;
2069
        tb.modtime = vs->v9stat.mtime;
2070
        if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2071
            err = -errno;
2072
        }
2073
    }
2074

    
2075
    v9fs_wstat_post_utime(s, vs, err);
2076
    return;
2077

    
2078
out:
2079
    v9fs_stat_free(&vs->v9stat);
2080
    complete_pdu(s, vs->pdu, err);
2081
    qemu_free(vs);
2082
}
2083

    
2084
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2085
{
2086
    if (err == -1) {
2087
        err = -errno;
2088
    }
2089
    v9fs_stat_free(&vs->v9stat);
2090
    complete_pdu(s, vs->pdu, err);
2091
    qemu_free(vs);
2092
}
2093

    
2094
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2095
{
2096
    uint32_t v9_mode;
2097

    
2098
    if (err == -1) {
2099
        err = -errno;
2100
        goto out;
2101
    }
2102

    
2103
    v9_mode = stat_to_v9mode(&vs->stbuf);
2104

    
2105
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2106
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2107
            /* Attempting to change the type */
2108
            err = -EIO;
2109
            goto out;
2110
    }
2111

    
2112
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2113
                    &vs->v9stat.extension))) {
2114
            err = -errno;
2115
     }
2116
    v9fs_wstat_post_chmod(s, vs, err);
2117
    return;
2118

    
2119
out:
2120
    v9fs_stat_free(&vs->v9stat);
2121
    complete_pdu(s, vs->pdu, err);
2122
    qemu_free(vs);
2123
}
2124

    
2125
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2126
{
2127
    int32_t fid;
2128
    V9fsWstatState *vs;
2129
    int err = 0;
2130

    
2131
    vs = qemu_malloc(sizeof(*vs));
2132
    vs->pdu = pdu;
2133
    vs->offset = 7;
2134

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

    
2137
    vs->fidp = lookup_fid(s, fid);
2138
    if (vs->fidp == NULL) {
2139
        err = -EINVAL;
2140
        goto out;
2141
    }
2142

    
2143
    /* do we need to sync the file? */
2144
    if (donttouch_stat(&vs->v9stat)) {
2145
        err = v9fs_do_fsync(s, vs->fidp->fd);
2146
        v9fs_wstat_post_fsync(s, vs, err);
2147
        return;
2148
    }
2149

    
2150
    if (vs->v9stat.mode != -1) {
2151
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2152
        v9fs_wstat_post_lstat(s, vs, err);
2153
        return;
2154
    }
2155

    
2156
    v9fs_wstat_post_chmod(s, vs, err);
2157
    return;
2158

    
2159
out:
2160
    v9fs_stat_free(&vs->v9stat);
2161
    complete_pdu(s, vs->pdu, err);
2162
    qemu_free(vs);
2163
}
2164

    
2165
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2166

    
2167
static pdu_handler_t *pdu_handlers[] = {
2168
    [P9_TVERSION] = v9fs_version,
2169
    [P9_TATTACH] = v9fs_attach,
2170
    [P9_TSTAT] = v9fs_stat,
2171
    [P9_TWALK] = v9fs_walk,
2172
    [P9_TCLUNK] = v9fs_clunk,
2173
    [P9_TOPEN] = v9fs_open,
2174
    [P9_TREAD] = v9fs_read,
2175
#if 0
2176
    [P9_TAUTH] = v9fs_auth,
2177
#endif
2178
    [P9_TFLUSH] = v9fs_flush,
2179
    [P9_TCREATE] = v9fs_create,
2180
    [P9_TWRITE] = v9fs_write,
2181
    [P9_TWSTAT] = v9fs_wstat,
2182
    [P9_TREMOVE] = v9fs_remove,
2183
};
2184

    
2185
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2186
{
2187
    pdu_handler_t *handler;
2188

    
2189
    if (debug_9p_pdu) {
2190
        pprint_pdu(pdu);
2191
    }
2192

    
2193
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2194

    
2195
    handler = pdu_handlers[pdu->id];
2196
    BUG_ON(handler == NULL);
2197

    
2198
    handler(s, pdu);
2199
}
2200

    
2201
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2202
{
2203
    V9fsState *s = (V9fsState *)vdev;
2204
    V9fsPDU *pdu;
2205
    ssize_t len;
2206

    
2207
    while ((pdu = alloc_pdu(s)) &&
2208
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2209
        uint8_t *ptr;
2210

    
2211
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2212
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2213

    
2214
        ptr = pdu->elem.out_sg[0].iov_base;
2215

    
2216
        memcpy(&pdu->size, ptr, 4);
2217
        pdu->id = ptr[4];
2218
        memcpy(&pdu->tag, ptr + 5, 2);
2219

    
2220
        submit_pdu(s, pdu);
2221
    }
2222

    
2223
    free_pdu(s, pdu);
2224
}
2225

    
2226
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2227
{
2228
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2229
    return features;
2230
}
2231

    
2232
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2233
{
2234
    return (V9fsState *)vdev;
2235
}
2236

    
2237
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2238
{
2239
    struct virtio_9p_config *cfg;
2240
    V9fsState *s = to_virtio_9p(vdev);
2241

    
2242
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2243
                        s->tag_len);
2244
    stw_raw(&cfg->tag_len, s->tag_len);
2245
    memcpy(cfg->tag, s->tag, s->tag_len);
2246
    memcpy(config, cfg, s->config_size);
2247
    qemu_free(cfg);
2248
}
2249

    
2250
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2251
 {
2252
    V9fsState *s;
2253
    int i, len;
2254
    struct stat stat;
2255
    FsTypeEntry *fse;
2256

    
2257

    
2258
    s = (V9fsState *)virtio_common_init("virtio-9p",
2259
                                    VIRTIO_ID_9P,
2260
                                    sizeof(struct virtio_9p_config)+
2261
                                    MAX_TAG_LEN,
2262
                                    sizeof(V9fsState));
2263

    
2264
    /* initialize pdu allocator */
2265
    QLIST_INIT(&s->free_list);
2266
    for (i = 0; i < (MAX_REQ - 1); i++) {
2267
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2268
    }
2269

    
2270
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2271

    
2272
    fse = get_fsdev_fsentry(conf->fsdev_id);
2273

    
2274
    if (!fse) {
2275
        /* We don't have a fsdev identified by fsdev_id */
2276
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2277
                    "with the id %s\n", conf->fsdev_id);
2278
        exit(1);
2279
    }
2280

    
2281
    if (!fse->path || !conf->tag) {
2282
        /* we haven't specified a mount_tag or the path */
2283
        fprintf(stderr, "fsdev with id %s needs path "
2284
                "and Virtio-9p device needs mount_tag arguments\n",
2285
                conf->fsdev_id);
2286
        exit(1);
2287
    }
2288

    
2289
    if (!strcmp(fse->security_model, "passthrough")) {
2290
        /* Files on the Fileserver set to client user credentials */
2291
        s->ctx.fs_sm = SM_PASSTHROUGH;
2292
    } else if (!strcmp(fse->security_model, "mapped")) {
2293
        /* Files on the fileserver are set to QEMU credentials.
2294
         * Client user credentials are saved in extended attributes.
2295
         */
2296
        s->ctx.fs_sm = SM_MAPPED;
2297
    } else {
2298
        /* user haven't specified a correct security option */
2299
        fprintf(stderr, "one of the following must be specified as the"
2300
                "security option:\n\t security_model=passthrough \n\t "
2301
                "security_model=mapped\n");
2302
        return NULL;
2303
    }
2304

    
2305
    if (lstat(fse->path, &stat)) {
2306
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2307
        exit(1);
2308
    } else if (!S_ISDIR(stat.st_mode)) {
2309
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2310
        exit(1);
2311
    }
2312

    
2313
    s->ctx.fs_root = qemu_strdup(fse->path);
2314
    len = strlen(conf->tag);
2315
    if (len > MAX_TAG_LEN) {
2316
        len = MAX_TAG_LEN;
2317
    }
2318
    /* s->tag is non-NULL terminated string */
2319
    s->tag = qemu_malloc(len);
2320
    memcpy(s->tag, conf->tag, len);
2321
    s->tag_len = len;
2322
    s->ctx.uid = -1;
2323

    
2324
    s->ops = fse->ops;
2325
    s->vdev.get_features = virtio_9p_get_features;
2326
    s->config_size = sizeof(struct virtio_9p_config) +
2327
                        s->tag_len;
2328
    s->vdev.get_config = virtio_9p_get_config;
2329

    
2330
    return &s->vdev;
2331
}