Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 74bc02b2

History | View | Annotate | Download (66 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_mkdir(V9fsState *s, V9fsCreateState *vs)
175
{
176
    FsCred cred;
177

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

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

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

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

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

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

    
203
static int v9fs_do_symlink(V9fsState *s, V9fsCreateState *vs)
204
{
205
    FsCred cred;
206
    cred_init(&cred);
207
    cred.fc_uid = vs->fidp->uid;
208
    cred.fc_mode = vs->perm | 0777;
209

    
210
    return s->ops->symlink(&s->ctx, vs->extension.data, vs->fullname.data,
211
            &cred);
212
}
213

    
214
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
215
{
216
    return s->ops->link(&s->ctx, oldpath->data, newpath->data);
217
}
218

    
219
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
220
{
221
    return s->ops->truncate(&s->ctx, path->data, size);
222
}
223

    
224
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
225
                            V9fsString *newpath)
226
{
227
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
228
}
229

    
230
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
231
{
232
    FsCred cred;
233
    cred_init(&cred);
234
    cred.fc_uid = uid;
235
    cred.fc_gid = gid;
236

    
237
    return s->ops->chown(&s->ctx, path->data, &cred);
238
}
239

    
240
static int v9fs_do_utimensat(V9fsState *s, V9fsString *path, V9fsStat v9stat)
241
{
242
    struct timespec ts[2];
243

    
244
    if (v9stat.atime != -1) {
245
        ts[0].tv_sec = v9stat.atime;
246
        ts[0].tv_nsec = 0;
247
    } else {
248
        ts[0].tv_nsec = UTIME_OMIT;
249
    }
250

    
251
    if (v9stat.mtime != -1) {
252
        ts[1].tv_sec = v9stat.mtime;
253
        ts[1].tv_nsec = 0;
254
    } else {
255
        ts[1].tv_nsec = UTIME_OMIT;
256
    }
257

    
258
    return s->ops->utimensat(&s->ctx, path->data, ts);
259
}
260

    
261
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
262
{
263
    return s->ops->remove(&s->ctx, path->data);
264
}
265

    
266
static int v9fs_do_fsync(V9fsState *s, int fd)
267
{
268
    return s->ops->fsync(&s->ctx, fd);
269
}
270

    
271
static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
272
{
273
    return s->ops->statfs(&s->ctx, path->data, stbuf);
274
}
275

    
276
static void v9fs_string_init(V9fsString *str)
277
{
278
    str->data = NULL;
279
    str->size = 0;
280
}
281

    
282
static void v9fs_string_free(V9fsString *str)
283
{
284
    qemu_free(str->data);
285
    str->data = NULL;
286
    str->size = 0;
287
}
288

    
289
static void v9fs_string_null(V9fsString *str)
290
{
291
    v9fs_string_free(str);
292
}
293

    
294
static int number_to_string(void *arg, char type)
295
{
296
    unsigned int ret = 0;
297

    
298
    switch (type) {
299
    case 'u': {
300
        unsigned int num = *(unsigned int *)arg;
301

    
302
        do {
303
            ret++;
304
            num = num/10;
305
        } while (num);
306
        break;
307
    }
308
    default:
309
        printf("Number_to_string: Unknown number format\n");
310
        return -1;
311
    }
312

    
313
    return ret;
314
}
315

    
316
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
317
{
318
    va_list ap2;
319
    char *iter = (char *)fmt;
320
    int len = 0;
321
    int nr_args = 0;
322
    char *arg_char_ptr;
323
    unsigned int arg_uint;
324

    
325
    /* Find the number of %'s that denotes an argument */
326
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
327
        nr_args++;
328
        iter++;
329
    }
330

    
331
    len = strlen(fmt) - 2*nr_args;
332

    
333
    if (!nr_args) {
334
        goto alloc_print;
335
    }
336

    
337
    va_copy(ap2, ap);
338

    
339
    iter = (char *)fmt;
340

    
341
    /* Now parse the format string */
342
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
343
        iter++;
344
        switch (*iter) {
345
        case 'u':
346
            arg_uint = va_arg(ap2, unsigned int);
347
            len += number_to_string((void *)&arg_uint, 'u');
348
            break;
349
        case 's':
350
            arg_char_ptr = va_arg(ap2, char *);
351
            len += strlen(arg_char_ptr);
352
            break;
353
        case 'c':
354
            len += 1;
355
            break;
356
        default:
357
            fprintf(stderr,
358
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
359
            return -1;
360
        }
361
        iter++;
362
    }
363

    
364
alloc_print:
365
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
366

    
367
    return vsprintf(*strp, fmt, ap);
368
}
369

    
370
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
371
{
372
    va_list ap;
373
    int err;
374

    
375
    v9fs_string_free(str);
376

    
377
    va_start(ap, fmt);
378
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
379
    BUG_ON(err == -1);
380
    va_end(ap);
381

    
382
    str->size = err;
383
}
384

    
385
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
386
{
387
    v9fs_string_free(lhs);
388
    v9fs_string_sprintf(lhs, "%s", rhs->data);
389
}
390

    
391
static size_t v9fs_string_size(V9fsString *str)
392
{
393
    return str->size;
394
}
395

    
396
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
397
{
398
    V9fsFidState *f;
399

    
400
    for (f = s->fid_list; f; f = f->next) {
401
        if (f->fid == fid) {
402
            return f;
403
        }
404
    }
405

    
406
    return NULL;
407
}
408

    
409
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
410
{
411
    V9fsFidState *f;
412

    
413
    f = lookup_fid(s, fid);
414
    if (f) {
415
        return NULL;
416
    }
417

    
418
    f = qemu_mallocz(sizeof(V9fsFidState));
419

    
420
    f->fid = fid;
421
    f->fd = -1;
422
    f->dir = NULL;
423

    
424
    f->next = s->fid_list;
425
    s->fid_list = f;
426

    
427
    return f;
428
}
429

    
430
static int free_fid(V9fsState *s, int32_t fid)
431
{
432
    V9fsFidState **fidpp, *fidp;
433

    
434
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
435
        if ((*fidpp)->fid == fid) {
436
            break;
437
        }
438
    }
439

    
440
    if (*fidpp == NULL) {
441
        return -ENOENT;
442
    }
443

    
444
    fidp = *fidpp;
445
    *fidpp = fidp->next;
446

    
447
    if (fidp->fd != -1) {
448
        v9fs_do_close(s, fidp->fd);
449
    }
450
    if (fidp->dir) {
451
        v9fs_do_closedir(s, fidp->dir);
452
    }
453
    v9fs_string_free(&fidp->path);
454
    qemu_free(fidp);
455

    
456
    return 0;
457
}
458

    
459
#define P9_QID_TYPE_DIR         0x80
460
#define P9_QID_TYPE_SYMLINK     0x02
461

    
462
#define P9_STAT_MODE_DIR        0x80000000
463
#define P9_STAT_MODE_APPEND     0x40000000
464
#define P9_STAT_MODE_EXCL       0x20000000
465
#define P9_STAT_MODE_MOUNT      0x10000000
466
#define P9_STAT_MODE_AUTH       0x08000000
467
#define P9_STAT_MODE_TMP        0x04000000
468
#define P9_STAT_MODE_SYMLINK    0x02000000
469
#define P9_STAT_MODE_LINK       0x01000000
470
#define P9_STAT_MODE_DEVICE     0x00800000
471
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
472
#define P9_STAT_MODE_SOCKET     0x00100000
473
#define P9_STAT_MODE_SETUID     0x00080000
474
#define P9_STAT_MODE_SETGID     0x00040000
475
#define P9_STAT_MODE_SETVTX     0x00010000
476

    
477
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
478
                                P9_STAT_MODE_SYMLINK |      \
479
                                P9_STAT_MODE_LINK |         \
480
                                P9_STAT_MODE_DEVICE |       \
481
                                P9_STAT_MODE_NAMED_PIPE |   \
482
                                P9_STAT_MODE_SOCKET)
483

    
484
/* This is the algorithm from ufs in spfs */
485
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
486
{
487
    size_t size;
488

    
489
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
490
    memcpy(&qidp->path, &stbuf->st_ino, size);
491
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
492
    qidp->type = 0;
493
    if (S_ISDIR(stbuf->st_mode)) {
494
        qidp->type |= P9_QID_TYPE_DIR;
495
    }
496
    if (S_ISLNK(stbuf->st_mode)) {
497
        qidp->type |= P9_QID_TYPE_SYMLINK;
498
    }
499
}
500

    
501
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
502
{
503
    struct stat stbuf;
504
    int err;
505

    
506
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
507
    if (err) {
508
        return err;
509
    }
510

    
511
    stat_to_qid(&stbuf, qidp);
512
    return 0;
513
}
514

    
515
static V9fsPDU *alloc_pdu(V9fsState *s)
516
{
517
    V9fsPDU *pdu = NULL;
518

    
519
    if (!QLIST_EMPTY(&s->free_list)) {
520
        pdu = QLIST_FIRST(&s->free_list);
521
        QLIST_REMOVE(pdu, next);
522
    }
523
    return pdu;
524
}
525

    
526
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
527
{
528
    if (pdu) {
529
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
530
    }
531
}
532

    
533
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
534
                        size_t offset, size_t size, int pack)
535
{
536
    int i = 0;
537
    size_t copied = 0;
538

    
539
    for (i = 0; size && i < sg_count; i++) {
540
        size_t len;
541
        if (offset >= sg[i].iov_len) {
542
            /* skip this sg */
543
            offset -= sg[i].iov_len;
544
            continue;
545
        } else {
546
            len = MIN(sg[i].iov_len - offset, size);
547
            if (pack) {
548
                memcpy(sg[i].iov_base + offset, addr, len);
549
            } else {
550
                memcpy(addr, sg[i].iov_base + offset, len);
551
            }
552
            size -= len;
553
            copied += len;
554
            addr += len;
555
            if (size) {
556
                offset = 0;
557
                continue;
558
            }
559
        }
560
    }
561

    
562
    return copied;
563
}
564

    
565
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
566
{
567
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
568
                         offset, size, 0);
569
}
570

    
571
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
572
                        size_t size)
573
{
574
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
575
                             offset, size, 1);
576
}
577

    
578
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
579
{
580
    size_t pos = 0;
581
    int i, j;
582
    struct iovec *src_sg;
583
    unsigned int num;
584

    
585
    if (rx) {
586
        src_sg = pdu->elem.in_sg;
587
        num = pdu->elem.in_num;
588
    } else {
589
        src_sg = pdu->elem.out_sg;
590
        num = pdu->elem.out_num;
591
    }
592

    
593
    j = 0;
594
    for (i = 0; i < num; i++) {
595
        if (offset <= pos) {
596
            sg[j].iov_base = src_sg[i].iov_base;
597
            sg[j].iov_len = src_sg[i].iov_len;
598
            j++;
599
        } else if (offset < (src_sg[i].iov_len + pos)) {
600
            sg[j].iov_base = src_sg[i].iov_base;
601
            sg[j].iov_len = src_sg[i].iov_len;
602
            sg[j].iov_base += (offset - pos);
603
            sg[j].iov_len -= (offset - pos);
604
            j++;
605
        }
606
        pos += src_sg[i].iov_len;
607
    }
608

    
609
    return j;
610
}
611

    
612
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
613
{
614
    size_t old_offset = offset;
615
    va_list ap;
616
    int i;
617

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

    
688
    va_end(ap);
689

    
690
    return offset - old_offset;
691
}
692

    
693
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
694
{
695
    size_t old_offset = offset;
696
    va_list ap;
697
    int i;
698

    
699
    va_start(ap, fmt);
700
    for (i = 0; fmt[i]; i++) {
701
        switch (fmt[i]) {
702
        case 'b': {
703
            uint8_t val = va_arg(ap, int);
704
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
705
            break;
706
        }
707
        case 'w': {
708
            uint16_t val;
709
            cpu_to_le16w(&val, va_arg(ap, int));
710
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
711
            break;
712
        }
713
        case 'd': {
714
            uint32_t val;
715
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
716
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
717
            break;
718
        }
719
        case 'q': {
720
            uint64_t val;
721
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
722
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
723
            break;
724
        }
725
        case 'v': {
726
            struct iovec *iov = va_arg(ap, struct iovec *);
727
            int *iovcnt = va_arg(ap, int *);
728
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
729
            break;
730
        }
731
        case 's': {
732
            V9fsString *str = va_arg(ap, V9fsString *);
733
            offset += pdu_marshal(pdu, offset, "w", str->size);
734
            offset += pdu_pack(pdu, offset, str->data, str->size);
735
            break;
736
        }
737
        case 'Q': {
738
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
739
            offset += pdu_marshal(pdu, offset, "bdq",
740
                        qidp->type, qidp->version, qidp->path);
741
            break;
742
        }
743
        case 'S': {
744
            V9fsStat *statp = va_arg(ap, V9fsStat *);
745
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
746
                        statp->size, statp->type, statp->dev,
747
                        &statp->qid, statp->mode, statp->atime,
748
                        statp->mtime, statp->length, &statp->name,
749
                        &statp->uid, &statp->gid, &statp->muid,
750
                        &statp->extension, statp->n_uid,
751
                        statp->n_gid, statp->n_muid);
752
            break;
753
        }
754
        case 'A': {
755
            V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
756
            offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
757
                        statp->st_result_mask,
758
                        &statp->qid, statp->st_mode,
759
                        statp->st_uid, statp->st_gid,
760
                        statp->st_nlink, statp->st_rdev,
761
                        statp->st_size, statp->st_blksize, statp->st_blocks,
762
                        statp->st_atime_sec, statp->st_atime_nsec,
763
                        statp->st_mtime_sec, statp->st_mtime_nsec,
764
                        statp->st_ctime_sec, statp->st_ctime_nsec,
765
                        statp->st_btime_sec, statp->st_btime_nsec,
766
                        statp->st_gen, statp->st_data_version);
767
            break;
768
        }
769
        default:
770
            break;
771
        }
772
    }
773
    va_end(ap);
774

    
775
    return offset - old_offset;
776
}
777

    
778
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
779
{
780
    int8_t id = pdu->id + 1; /* Response */
781

    
782
    if (len < 0) {
783
        V9fsString str;
784
        int err = -len;
785

    
786
        str.data = strerror(err);
787
        str.size = strlen(str.data);
788

    
789
        len = 7;
790
        len += pdu_marshal(pdu, len, "s", &str);
791
        if (dotu) {
792
            len += pdu_marshal(pdu, len, "d", err);
793
        }
794

    
795
        id = P9_RERROR;
796
    }
797

    
798
    /* fill out the header */
799
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
800

    
801
    /* keep these in sync */
802
    pdu->size = len;
803
    pdu->id = id;
804

    
805
    /* push onto queue and notify */
806
    virtqueue_push(s->vq, &pdu->elem, len);
807

    
808
    /* FIXME: we should batch these completions */
809
    virtio_notify(&s->vdev, s->vq);
810

    
811
    free_pdu(s, pdu);
812
}
813

    
814
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
815
{
816
    mode_t ret;
817

    
818
    ret = mode & 0777;
819
    if (mode & P9_STAT_MODE_DIR) {
820
        ret |= S_IFDIR;
821
    }
822

    
823
    if (dotu) {
824
        if (mode & P9_STAT_MODE_SYMLINK) {
825
            ret |= S_IFLNK;
826
        }
827
        if (mode & P9_STAT_MODE_SOCKET) {
828
            ret |= S_IFSOCK;
829
        }
830
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
831
            ret |= S_IFIFO;
832
        }
833
        if (mode & P9_STAT_MODE_DEVICE) {
834
            if (extension && extension->data[0] == 'c') {
835
                ret |= S_IFCHR;
836
            } else {
837
                ret |= S_IFBLK;
838
            }
839
        }
840
    }
841

    
842
    if (!(ret&~0777)) {
843
        ret |= S_IFREG;
844
    }
845

    
846
    if (mode & P9_STAT_MODE_SETUID) {
847
        ret |= S_ISUID;
848
    }
849
    if (mode & P9_STAT_MODE_SETGID) {
850
        ret |= S_ISGID;
851
    }
852
    if (mode & P9_STAT_MODE_SETVTX) {
853
        ret |= S_ISVTX;
854
    }
855

    
856
    return ret;
857
}
858

    
859
static int donttouch_stat(V9fsStat *stat)
860
{
861
    if (stat->type == -1 &&
862
        stat->dev == -1 &&
863
        stat->qid.type == -1 &&
864
        stat->qid.version == -1 &&
865
        stat->qid.path == -1 &&
866
        stat->mode == -1 &&
867
        stat->atime == -1 &&
868
        stat->mtime == -1 &&
869
        stat->length == -1 &&
870
        !stat->name.size &&
871
        !stat->uid.size &&
872
        !stat->gid.size &&
873
        !stat->muid.size &&
874
        stat->n_uid == -1 &&
875
        stat->n_gid == -1 &&
876
        stat->n_muid == -1) {
877
        return 1;
878
    }
879

    
880
    return 0;
881
}
882

    
883
static void v9fs_stat_free(V9fsStat *stat)
884
{
885
    v9fs_string_free(&stat->name);
886
    v9fs_string_free(&stat->uid);
887
    v9fs_string_free(&stat->gid);
888
    v9fs_string_free(&stat->muid);
889
    v9fs_string_free(&stat->extension);
890
}
891

    
892
static uint32_t stat_to_v9mode(const struct stat *stbuf)
893
{
894
    uint32_t mode;
895

    
896
    mode = stbuf->st_mode & 0777;
897
    if (S_ISDIR(stbuf->st_mode)) {
898
        mode |= P9_STAT_MODE_DIR;
899
    }
900

    
901
    if (dotu) {
902
        if (S_ISLNK(stbuf->st_mode)) {
903
            mode |= P9_STAT_MODE_SYMLINK;
904
        }
905

    
906
        if (S_ISSOCK(stbuf->st_mode)) {
907
            mode |= P9_STAT_MODE_SOCKET;
908
        }
909

    
910
        if (S_ISFIFO(stbuf->st_mode)) {
911
            mode |= P9_STAT_MODE_NAMED_PIPE;
912
        }
913

    
914
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
915
            mode |= P9_STAT_MODE_DEVICE;
916
        }
917

    
918
        if (stbuf->st_mode & S_ISUID) {
919
            mode |= P9_STAT_MODE_SETUID;
920
        }
921

    
922
        if (stbuf->st_mode & S_ISGID) {
923
            mode |= P9_STAT_MODE_SETGID;
924
        }
925

    
926
        if (stbuf->st_mode & S_ISVTX) {
927
            mode |= P9_STAT_MODE_SETVTX;
928
        }
929
    }
930

    
931
    return mode;
932
}
933

    
934
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
935
                            const struct stat *stbuf,
936
                            V9fsStat *v9stat)
937
{
938
    int err;
939
    const char *str;
940

    
941
    memset(v9stat, 0, sizeof(*v9stat));
942

    
943
    stat_to_qid(stbuf, &v9stat->qid);
944
    v9stat->mode = stat_to_v9mode(stbuf);
945
    v9stat->atime = stbuf->st_atime;
946
    v9stat->mtime = stbuf->st_mtime;
947
    v9stat->length = stbuf->st_size;
948

    
949
    v9fs_string_null(&v9stat->uid);
950
    v9fs_string_null(&v9stat->gid);
951
    v9fs_string_null(&v9stat->muid);
952

    
953
    if (dotu) {
954
        v9stat->n_uid = stbuf->st_uid;
955
        v9stat->n_gid = stbuf->st_gid;
956
        v9stat->n_muid = 0;
957

    
958
        v9fs_string_null(&v9stat->extension);
959

    
960
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
961
            err = v9fs_do_readlink(s, name, &v9stat->extension);
962
            if (err == -1) {
963
                err = -errno;
964
                return err;
965
            }
966
            v9stat->extension.data[err] = 0;
967
            v9stat->extension.size = err;
968
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
969
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
970
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
971
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
972
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
973
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
974
                    "HARDLINKCOUNT", stbuf->st_nlink);
975
        }
976
    }
977

    
978
    str = strrchr(name->data, '/');
979
    if (str) {
980
        str += 1;
981
    } else {
982
        str = name->data;
983
    }
984

    
985
    v9fs_string_sprintf(&v9stat->name, "%s", str);
986

    
987
    v9stat->size = 61 +
988
        v9fs_string_size(&v9stat->name) +
989
        v9fs_string_size(&v9stat->uid) +
990
        v9fs_string_size(&v9stat->gid) +
991
        v9fs_string_size(&v9stat->muid) +
992
        v9fs_string_size(&v9stat->extension);
993
    return 0;
994
}
995

    
996
#define P9_STATS_MODE          0x00000001ULL
997
#define P9_STATS_NLINK         0x00000002ULL
998
#define P9_STATS_UID           0x00000004ULL
999
#define P9_STATS_GID           0x00000008ULL
1000
#define P9_STATS_RDEV          0x00000010ULL
1001
#define P9_STATS_ATIME         0x00000020ULL
1002
#define P9_STATS_MTIME         0x00000040ULL
1003
#define P9_STATS_CTIME         0x00000080ULL
1004
#define P9_STATS_INO           0x00000100ULL
1005
#define P9_STATS_SIZE          0x00000200ULL
1006
#define P9_STATS_BLOCKS        0x00000400ULL
1007

    
1008
#define P9_STATS_BTIME         0x00000800ULL
1009
#define P9_STATS_GEN           0x00001000ULL
1010
#define P9_STATS_DATA_VERSION  0x00002000ULL
1011

    
1012
#define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
1013
#define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
1014

    
1015

    
1016
static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1017
                            V9fsStatDotl *v9lstat)
1018
{
1019
    memset(v9lstat, 0, sizeof(*v9lstat));
1020

    
1021
    v9lstat->st_mode = stbuf->st_mode;
1022
    v9lstat->st_nlink = stbuf->st_nlink;
1023
    v9lstat->st_uid = stbuf->st_uid;
1024
    v9lstat->st_gid = stbuf->st_gid;
1025
    v9lstat->st_rdev = stbuf->st_rdev;
1026
    v9lstat->st_size = stbuf->st_size;
1027
    v9lstat->st_blksize = stbuf->st_blksize;
1028
    v9lstat->st_blocks = stbuf->st_blocks;
1029
    v9lstat->st_atime_sec = stbuf->st_atime;
1030
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1031
    v9lstat->st_mtime_sec = stbuf->st_mtime;
1032
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1033
    v9lstat->st_ctime_sec = stbuf->st_ctime;
1034
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1035
    /* Currently we only support BASIC fields in stat */
1036
    v9lstat->st_result_mask = P9_STATS_BASIC;
1037

    
1038
    stat_to_qid(stbuf, &v9lstat->qid);
1039
}
1040

    
1041
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1042
{
1043
    while (len && *iovcnt) {
1044
        if (len < sg->iov_len) {
1045
            sg->iov_len -= len;
1046
            sg->iov_base += len;
1047
            len = 0;
1048
        } else {
1049
            len -= sg->iov_len;
1050
            sg++;
1051
            *iovcnt -= 1;
1052
        }
1053
    }
1054

    
1055
    return sg;
1056
}
1057

    
1058
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1059
{
1060
    int i;
1061
    int total = 0;
1062

    
1063
    for (i = 0; i < *cnt; i++) {
1064
        if ((total + sg[i].iov_len) > cap) {
1065
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1066
            i++;
1067
            break;
1068
        }
1069
        total += sg[i].iov_len;
1070
    }
1071

    
1072
    *cnt = i;
1073

    
1074
    return sg;
1075
}
1076

    
1077
static void print_sg(struct iovec *sg, int cnt)
1078
{
1079
    int i;
1080

    
1081
    printf("sg[%d]: {", cnt);
1082
    for (i = 0; i < cnt; i++) {
1083
        if (i) {
1084
            printf(", ");
1085
        }
1086
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1087
    }
1088
    printf("}\n");
1089
}
1090

    
1091
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1092
{
1093
    V9fsString str;
1094
    v9fs_string_init(&str);
1095
    v9fs_string_copy(&str, dst);
1096
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1097
    v9fs_string_free(&str);
1098
}
1099

    
1100
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1101
{
1102
    V9fsString version;
1103
    size_t offset = 7;
1104

    
1105
    pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1106

    
1107
    if (!strcmp(version.data, "9P2000.u")) {
1108
        s->proto_version = V9FS_PROTO_2000U;
1109
    } else if (!strcmp(version.data, "9P2000.L")) {
1110
        s->proto_version = V9FS_PROTO_2000L;
1111
    } else {
1112
        v9fs_string_sprintf(&version, "unknown");
1113
    }
1114

    
1115
    offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1116
    complete_pdu(s, pdu, offset);
1117

    
1118
    v9fs_string_free(&version);
1119
}
1120

    
1121
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1122
{
1123
    int32_t fid, afid, n_uname;
1124
    V9fsString uname, aname;
1125
    V9fsFidState *fidp;
1126
    V9fsQID qid;
1127
    size_t offset = 7;
1128
    ssize_t err;
1129

    
1130
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1131

    
1132
    fidp = alloc_fid(s, fid);
1133
    if (fidp == NULL) {
1134
        err = -EINVAL;
1135
        goto out;
1136
    }
1137

    
1138
    fidp->uid = n_uname;
1139

    
1140
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1141
    err = fid_to_qid(s, fidp, &qid);
1142
    if (err) {
1143
        err = -EINVAL;
1144
        free_fid(s, fid);
1145
        goto out;
1146
    }
1147

    
1148
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1149

    
1150
    err = offset;
1151
out:
1152
    complete_pdu(s, pdu, err);
1153
    v9fs_string_free(&uname);
1154
    v9fs_string_free(&aname);
1155
}
1156

    
1157
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1158
{
1159
    if (err == -1) {
1160
        err = -errno;
1161
        goto out;
1162
    }
1163

    
1164
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1165
    if (err) {
1166
        goto out;
1167
    }
1168
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1169
    err = vs->offset;
1170

    
1171
out:
1172
    complete_pdu(s, vs->pdu, err);
1173
    v9fs_stat_free(&vs->v9stat);
1174
    qemu_free(vs);
1175
}
1176

    
1177
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1178
{
1179
    int32_t fid;
1180
    V9fsStatState *vs;
1181
    ssize_t err = 0;
1182

    
1183
    vs = qemu_malloc(sizeof(*vs));
1184
    vs->pdu = pdu;
1185
    vs->offset = 7;
1186

    
1187
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1188

    
1189
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1190

    
1191
    vs->fidp = lookup_fid(s, fid);
1192
    if (vs->fidp == NULL) {
1193
        err = -ENOENT;
1194
        goto out;
1195
    }
1196

    
1197
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1198
    v9fs_stat_post_lstat(s, vs, err);
1199
    return;
1200

    
1201
out:
1202
    complete_pdu(s, vs->pdu, err);
1203
    v9fs_stat_free(&vs->v9stat);
1204
    qemu_free(vs);
1205
}
1206

    
1207
static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1208
                                                                int err)
1209
{
1210
    if (err == -1) {
1211
        err = -errno;
1212
        goto out;
1213
    }
1214

    
1215
    stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1216
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1217
    err = vs->offset;
1218

    
1219
out:
1220
    complete_pdu(s, vs->pdu, err);
1221
    qemu_free(vs);
1222
}
1223

    
1224
static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1225
{
1226
    int32_t fid;
1227
    V9fsStatStateDotl *vs;
1228
    ssize_t err = 0;
1229
    V9fsFidState *fidp;
1230
    uint64_t request_mask;
1231

    
1232
    vs = qemu_malloc(sizeof(*vs));
1233
    vs->pdu = pdu;
1234
    vs->offset = 7;
1235

    
1236
    memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1237

    
1238
    pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1239

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

    
1246
    /* Currently we only support BASIC fields in stat, so there is no
1247
     * need to look at request_mask.
1248
     */
1249
    err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1250
    v9fs_getattr_post_lstat(s, vs, err);
1251
    return;
1252

    
1253
out:
1254
    complete_pdu(s, vs->pdu, err);
1255
    qemu_free(vs);
1256
}
1257

    
1258
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1259
{
1260
    complete_pdu(s, vs->pdu, err);
1261

    
1262
    if (vs->nwnames) {
1263
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1264
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1265
        }
1266

    
1267
        qemu_free(vs->wnames);
1268
        qemu_free(vs->qids);
1269
    }
1270
}
1271

    
1272
static void v9fs_walk_marshal(V9fsWalkState *vs)
1273
{
1274
    int i;
1275
    vs->offset = 7;
1276
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1277

    
1278
    for (i = 0; i < vs->nwnames; i++) {
1279
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1280
    }
1281
}
1282

    
1283
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1284
                                                                int err)
1285
{
1286
    if (err == -1) {
1287
        free_fid(s, vs->newfidp->fid);
1288
        v9fs_string_free(&vs->path);
1289
        err = -ENOENT;
1290
        goto out;
1291
    }
1292

    
1293
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1294

    
1295
    vs->name_idx++;
1296
    if (vs->name_idx < vs->nwnames) {
1297
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1298
                                            vs->wnames[vs->name_idx].data);
1299
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1300

    
1301
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1302
        v9fs_walk_post_newfid_lstat(s, vs, err);
1303
        return;
1304
    }
1305

    
1306
    v9fs_string_free(&vs->path);
1307
    v9fs_walk_marshal(vs);
1308
    err = vs->offset;
1309
out:
1310
    v9fs_walk_complete(s, vs, err);
1311
}
1312

    
1313
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1314
        int err)
1315
{
1316
    if (err == -1) {
1317
        v9fs_string_free(&vs->path);
1318
        err = -ENOENT;
1319
        goto out;
1320
    }
1321

    
1322
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1323
    vs->name_idx++;
1324
    if (vs->name_idx < vs->nwnames) {
1325

    
1326
        v9fs_string_sprintf(&vs->path, "%s/%s",
1327
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1328
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1329

    
1330
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1331
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1332
        return;
1333
    }
1334

    
1335
    v9fs_string_free(&vs->path);
1336
    v9fs_walk_marshal(vs);
1337
    err = vs->offset;
1338
out:
1339
    v9fs_walk_complete(s, vs, err);
1340
}
1341

    
1342
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1343
{
1344
    int32_t fid, newfid;
1345
    V9fsWalkState *vs;
1346
    int err = 0;
1347
    int i;
1348

    
1349
    vs = qemu_malloc(sizeof(*vs));
1350
    vs->pdu = pdu;
1351
    vs->wnames = NULL;
1352
    vs->qids = NULL;
1353
    vs->offset = 7;
1354

    
1355
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1356
                                            &newfid, &vs->nwnames);
1357

    
1358
    if (vs->nwnames) {
1359
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1360

    
1361
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1362

    
1363
        for (i = 0; i < vs->nwnames; i++) {
1364
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1365
                                            &vs->wnames[i]);
1366
        }
1367
    }
1368

    
1369
    vs->fidp = lookup_fid(s, fid);
1370
    if (vs->fidp == NULL) {
1371
        err = -ENOENT;
1372
        goto out;
1373
    }
1374

    
1375
    /* FIXME: is this really valid? */
1376
    if (fid == newfid) {
1377

    
1378
        BUG_ON(vs->fidp->fd != -1);
1379
        BUG_ON(vs->fidp->dir);
1380
        v9fs_string_init(&vs->path);
1381
        vs->name_idx = 0;
1382

    
1383
        if (vs->name_idx < vs->nwnames) {
1384
            v9fs_string_sprintf(&vs->path, "%s/%s",
1385
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1386
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1387

    
1388
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1389
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1390
            return;
1391
        }
1392
    } else {
1393
        vs->newfidp = alloc_fid(s, newfid);
1394
        if (vs->newfidp == NULL) {
1395
            err = -EINVAL;
1396
            goto out;
1397
        }
1398

    
1399
        vs->newfidp->uid = vs->fidp->uid;
1400
        v9fs_string_init(&vs->path);
1401
        vs->name_idx = 0;
1402
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1403

    
1404
        if (vs->name_idx < vs->nwnames) {
1405
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1406
                                vs->wnames[vs->name_idx].data);
1407
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1408

    
1409
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1410
            v9fs_walk_post_newfid_lstat(s, vs, err);
1411
            return;
1412
        }
1413
    }
1414

    
1415
    v9fs_walk_marshal(vs);
1416
    err = vs->offset;
1417
out:
1418
    v9fs_walk_complete(s, vs, err);
1419
}
1420

    
1421
static int32_t get_iounit(V9fsState *s, V9fsString *name)
1422
{
1423
    struct statfs stbuf;
1424
    int32_t iounit = 0;
1425

    
1426
    /*
1427
     * iounit should be multiples of f_bsize (host filesystem block size
1428
     * and as well as less than (client msize - P9_IOHDRSZ))
1429
     */
1430
    if (!v9fs_do_statfs(s, name, &stbuf)) {
1431
        iounit = stbuf.f_bsize;
1432
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1433
    }
1434

    
1435
    if (!iounit) {
1436
        iounit = s->msize - P9_IOHDRSZ;
1437
    }
1438
    return iounit;
1439
}
1440

    
1441
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1442
{
1443
    if (vs->fidp->dir == NULL) {
1444
        err = -errno;
1445
        goto out;
1446
    }
1447

    
1448
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1449
    err = vs->offset;
1450
out:
1451
    complete_pdu(s, vs->pdu, err);
1452
    qemu_free(vs);
1453

    
1454
}
1455

    
1456
static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1457
{
1458
    int err;
1459
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1460
    err = vs->offset;
1461
    complete_pdu(s, vs->pdu, err);
1462
    qemu_free(vs);
1463
}
1464

    
1465
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1466
{
1467
    if (vs->fidp->fd == -1) {
1468
        err = -errno;
1469
        goto out;
1470
    }
1471

    
1472
    vs->iounit = get_iounit(s, &vs->fidp->path);
1473
    v9fs_open_post_getiounit(s, vs);
1474
    return;
1475
out:
1476
    complete_pdu(s, vs->pdu, err);
1477
    qemu_free(vs);
1478
}
1479

    
1480
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1481
{
1482
    if (err) {
1483
        err = -errno;
1484
        goto out;
1485
    }
1486

    
1487
    stat_to_qid(&vs->stbuf, &vs->qid);
1488

    
1489
    if (S_ISDIR(vs->stbuf.st_mode)) {
1490
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1491
        v9fs_open_post_opendir(s, vs, err);
1492
    } else {
1493
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1494
                                    omode_to_uflags(vs->mode));
1495
        v9fs_open_post_open(s, vs, err);
1496
    }
1497
    return;
1498
out:
1499
    complete_pdu(s, vs->pdu, err);
1500
    qemu_free(vs);
1501
}
1502

    
1503
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1504
{
1505
    int32_t fid;
1506
    V9fsOpenState *vs;
1507
    ssize_t err = 0;
1508

    
1509

    
1510
    vs = qemu_malloc(sizeof(*vs));
1511
    vs->pdu = pdu;
1512
    vs->offset = 7;
1513

    
1514
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1515

    
1516
    vs->fidp = lookup_fid(s, fid);
1517
    if (vs->fidp == NULL) {
1518
        err = -ENOENT;
1519
        goto out;
1520
    }
1521

    
1522
    BUG_ON(vs->fidp->fd != -1);
1523
    BUG_ON(vs->fidp->dir);
1524

    
1525
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1526

    
1527
    v9fs_open_post_lstat(s, vs, err);
1528
    return;
1529
out:
1530
    complete_pdu(s, pdu, err);
1531
    qemu_free(vs);
1532
}
1533

    
1534
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1535
{
1536
    int32_t fid;
1537
    size_t offset = 7;
1538
    int err;
1539

    
1540
    pdu_unmarshal(pdu, offset, "d", &fid);
1541

    
1542
    err = free_fid(s, fid);
1543
    if (err < 0) {
1544
        goto out;
1545
    }
1546

    
1547
    offset = 7;
1548
    err = offset;
1549
out:
1550
    complete_pdu(s, pdu, err);
1551
}
1552

    
1553
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1554

    
1555
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1556
{
1557
    if (err) {
1558
        goto out;
1559
    }
1560
    v9fs_stat_free(&vs->v9stat);
1561
    v9fs_string_free(&vs->name);
1562
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1563
    vs->offset += vs->count;
1564
    err = vs->offset;
1565
out:
1566
    complete_pdu(s, vs->pdu, err);
1567
    qemu_free(vs);
1568
    return;
1569
}
1570

    
1571
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1572
                                    ssize_t err)
1573
{
1574
    if (err) {
1575
        err = -errno;
1576
        goto out;
1577
    }
1578
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1579
    if (err) {
1580
        goto out;
1581
    }
1582

    
1583
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1584
                            &vs->v9stat);
1585
    if ((vs->len != (vs->v9stat.size + 2)) ||
1586
            ((vs->count + vs->len) > vs->max_count)) {
1587
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1588
        v9fs_read_post_seekdir(s, vs, err);
1589
        return;
1590
    }
1591
    vs->count += vs->len;
1592
    v9fs_stat_free(&vs->v9stat);
1593
    v9fs_string_free(&vs->name);
1594
    vs->dir_pos = vs->dent->d_off;
1595
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1596
    v9fs_read_post_readdir(s, vs, err);
1597
    return;
1598
out:
1599
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1600
    v9fs_read_post_seekdir(s, vs, err);
1601
    return;
1602

    
1603
}
1604

    
1605
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1606
{
1607
    if (vs->dent) {
1608
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1609
        v9fs_string_init(&vs->name);
1610
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1611
                            vs->dent->d_name);
1612
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1613
        v9fs_read_post_dir_lstat(s, vs, err);
1614
        return;
1615
    }
1616

    
1617
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1618
    vs->offset += vs->count;
1619
    err = vs->offset;
1620
    complete_pdu(s, vs->pdu, err);
1621
    qemu_free(vs);
1622
    return;
1623
}
1624

    
1625
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1626
{
1627
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1628
    v9fs_read_post_readdir(s, vs, err);
1629
    return;
1630
}
1631

    
1632
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1633
                                       ssize_t err)
1634
{
1635
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1636
    v9fs_read_post_telldir(s, vs, err);
1637
    return;
1638
}
1639

    
1640
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1641
{
1642
    if (err  < 0) {
1643
        /* IO error return the error */
1644
        err = -errno;
1645
        goto out;
1646
    }
1647
    vs->total += vs->len;
1648
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1649
    if (vs->total < vs->count && vs->len > 0) {
1650
        do {
1651
            if (0) {
1652
                print_sg(vs->sg, vs->cnt);
1653
            }
1654
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1655
        } while (vs->len == -1 && errno == EINTR);
1656
        if (vs->len == -1) {
1657
            err  = -errno;
1658
        }
1659
        v9fs_read_post_readv(s, vs, err);
1660
        return;
1661
    }
1662
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1663
    vs->offset += vs->count;
1664
    err = vs->offset;
1665

    
1666
out:
1667
    complete_pdu(s, vs->pdu, err);
1668
    qemu_free(vs);
1669
}
1670

    
1671
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1672
{
1673
    if (err == -1) {
1674
        err = -errno;
1675
        goto out;
1676
    }
1677
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1678

    
1679
    if (vs->total < vs->count) {
1680
        do {
1681
            if (0) {
1682
                print_sg(vs->sg, vs->cnt);
1683
            }
1684
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1685
        } while (vs->len == -1 && errno == EINTR);
1686
        if (vs->len == -1) {
1687
            err  = -errno;
1688
        }
1689
        v9fs_read_post_readv(s, vs, err);
1690
        return;
1691
    }
1692
out:
1693
    complete_pdu(s, vs->pdu, err);
1694
    qemu_free(vs);
1695
}
1696

    
1697
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1698
{
1699
    int32_t fid;
1700
    V9fsReadState *vs;
1701
    ssize_t err = 0;
1702

    
1703
    vs = qemu_malloc(sizeof(*vs));
1704
    vs->pdu = pdu;
1705
    vs->offset = 7;
1706
    vs->total = 0;
1707
    vs->len = 0;
1708
    vs->count = 0;
1709

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

    
1712
    vs->fidp = lookup_fid(s, fid);
1713
    if (vs->fidp == NULL) {
1714
        err = -EINVAL;
1715
        goto out;
1716
    }
1717

    
1718
    if (vs->fidp->dir) {
1719
        vs->max_count = vs->count;
1720
        vs->count = 0;
1721
        if (vs->off == 0) {
1722
            v9fs_do_rewinddir(s, vs->fidp->dir);
1723
        }
1724
        v9fs_read_post_rewinddir(s, vs, err);
1725
        return;
1726
    } else if (vs->fidp->fd != -1) {
1727
        vs->sg = vs->iov;
1728
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1729
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1730
        v9fs_read_post_lseek(s, vs, err);
1731
        return;
1732
    } else {
1733
        err = -EINVAL;
1734
    }
1735
out:
1736
    complete_pdu(s, pdu, err);
1737
    qemu_free(vs);
1738
}
1739

    
1740
typedef struct V9fsReadDirState {
1741
    V9fsPDU *pdu;
1742
    V9fsFidState *fidp;
1743
    V9fsQID qid;
1744
    off_t saved_dir_pos;
1745
    struct dirent *dent;
1746
    int32_t count;
1747
    int32_t max_count;
1748
    size_t offset;
1749
    int64_t initial_offset;
1750
    V9fsString name;
1751
} V9fsReadDirState;
1752

    
1753
static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
1754
{
1755
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1756
    vs->offset += vs->count;
1757
    complete_pdu(s, vs->pdu, vs->offset);
1758
    qemu_free(vs);
1759
    return;
1760
}
1761

    
1762
/* Size of each dirent on the wire: size of qid (13) + size of offset (8)
1763
 * size of type (1) + size of name.size (2) + strlen(name.data)
1764
 */
1765
#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
1766

    
1767
static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
1768
{
1769
    int len;
1770
    size_t size;
1771

    
1772
    if (vs->dent) {
1773
        v9fs_string_init(&vs->name);
1774
        v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
1775

    
1776
        if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
1777
            /* Ran out of buffer. Set dir back to old position and return */
1778
            v9fs_do_seekdir(s, vs->fidp->dir, vs->saved_dir_pos);
1779
            v9fs_readdir_post_seekdir(s, vs);
1780
            return;
1781
        }
1782

    
1783
        /* Fill up just the path field of qid because the client uses
1784
         * only that. To fill the entire qid structure we will have
1785
         * to stat each dirent found, which is expensive
1786
         */
1787
        size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
1788
        memcpy(&vs->qid.path, &vs->dent->d_ino, size);
1789
        /* Fill the other fields with dummy values */
1790
        vs->qid.type = 0;
1791
        vs->qid.version = 0;
1792

    
1793
        len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
1794
                              &vs->qid, vs->dent->d_off,
1795
                              vs->dent->d_type, &vs->name);
1796
        vs->count += len;
1797
        v9fs_string_free(&vs->name);
1798
        vs->saved_dir_pos = vs->dent->d_off;
1799
        vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1800
        v9fs_readdir_post_readdir(s, vs);
1801
        return;
1802
    }
1803

    
1804
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1805
    vs->offset += vs->count;
1806
    complete_pdu(s, vs->pdu, vs->offset);
1807
    qemu_free(vs);
1808
    return;
1809
}
1810

    
1811
static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
1812
{
1813
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1814
    v9fs_readdir_post_readdir(s, vs);
1815
    return;
1816
}
1817

    
1818
static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
1819
{
1820
    vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1821
    v9fs_readdir_post_telldir(s, vs);
1822
    return;
1823
}
1824

    
1825
static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
1826
{
1827
    int32_t fid;
1828
    V9fsReadDirState *vs;
1829
    ssize_t err = 0;
1830
    size_t offset = 7;
1831

    
1832
    vs = qemu_malloc(sizeof(*vs));
1833
    vs->pdu = pdu;
1834
    vs->offset = 7;
1835
    vs->count = 0;
1836

    
1837
    pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
1838
                                                        &vs->max_count);
1839

    
1840
    vs->fidp = lookup_fid(s, fid);
1841
    if (vs->fidp == NULL || !(vs->fidp->dir)) {
1842
        err = -EINVAL;
1843
        goto out;
1844
    }
1845

    
1846
    if (vs->initial_offset == 0) {
1847
        v9fs_do_rewinddir(s, vs->fidp->dir);
1848
    } else {
1849
        v9fs_do_seekdir(s, vs->fidp->dir, vs->initial_offset);
1850
    }
1851

    
1852
    v9fs_readdir_post_setdir(s, vs);
1853
    return;
1854

    
1855
out:
1856
    complete_pdu(s, pdu, err);
1857
    qemu_free(vs);
1858
    return;
1859
}
1860

    
1861
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1862
                                   ssize_t err)
1863
{
1864
    if (err  < 0) {
1865
        /* IO error return the error */
1866
        err = -errno;
1867
        goto out;
1868
    }
1869
    vs->total += vs->len;
1870
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1871
    if (vs->total < vs->count && vs->len > 0) {
1872
        do {
1873
            if (0) {
1874
                print_sg(vs->sg, vs->cnt);
1875
            }
1876
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1877
        } while (vs->len == -1 && errno == EINTR);
1878
        if (vs->len == -1) {
1879
            err  = -errno;
1880
        }
1881
        v9fs_write_post_writev(s, vs, err);
1882
        return;
1883
    }
1884
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1885

    
1886
    err = vs->offset;
1887
out:
1888
    complete_pdu(s, vs->pdu, err);
1889
    qemu_free(vs);
1890
}
1891

    
1892
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1893
{
1894
    if (err == -1) {
1895
        err = -errno;
1896
        goto out;
1897
    }
1898
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1899

    
1900
    if (vs->total < vs->count) {
1901
        do {
1902
            if (0) {
1903
                print_sg(vs->sg, vs->cnt);
1904
            }
1905
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1906
        } while (vs->len == -1 && errno == EINTR);
1907
        if (vs->len == -1) {
1908
            err  = -errno;
1909
        }
1910
        v9fs_write_post_writev(s, vs, err);
1911
        return;
1912
    }
1913

    
1914
out:
1915
    complete_pdu(s, vs->pdu, err);
1916
    qemu_free(vs);
1917
}
1918

    
1919
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1920
{
1921
    int32_t fid;
1922
    V9fsWriteState *vs;
1923
    ssize_t err;
1924

    
1925
    vs = qemu_malloc(sizeof(*vs));
1926

    
1927
    vs->pdu = pdu;
1928
    vs->offset = 7;
1929
    vs->sg = vs->iov;
1930
    vs->total = 0;
1931
    vs->len = 0;
1932

    
1933
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1934
                    vs->sg, &vs->cnt);
1935

    
1936
    vs->fidp = lookup_fid(s, fid);
1937
    if (vs->fidp == NULL) {
1938
        err = -EINVAL;
1939
        goto out;
1940
    }
1941

    
1942
    if (vs->fidp->fd == -1) {
1943
        err = -EINVAL;
1944
        goto out;
1945
    }
1946

    
1947
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1948

    
1949
    v9fs_write_post_lseek(s, vs, err);
1950
    return;
1951

    
1952
out:
1953
    complete_pdu(s, vs->pdu, err);
1954
    qemu_free(vs);
1955
}
1956

    
1957
static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
1958
{
1959
    int err;
1960
    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1961
    stat_to_qid(&vs->stbuf, &vs->qid);
1962

    
1963
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1964
    err = vs->offset;
1965

    
1966
    complete_pdu(s, vs->pdu, err);
1967
    v9fs_string_free(&vs->name);
1968
    v9fs_string_free(&vs->extension);
1969
    v9fs_string_free(&vs->fullname);
1970
    qemu_free(vs);
1971
}
1972

    
1973
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1974
{
1975
    if (err == 0) {
1976
        vs->iounit = get_iounit(s, &vs->fidp->path);
1977
        v9fs_create_post_getiounit(s, vs);
1978
        return;
1979
    }
1980

    
1981
    complete_pdu(s, vs->pdu, err);
1982
    v9fs_string_free(&vs->name);
1983
    v9fs_string_free(&vs->extension);
1984
    v9fs_string_free(&vs->fullname);
1985
    qemu_free(vs);
1986
}
1987

    
1988
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1989
{
1990
    if (err) {
1991
        err = -errno;
1992
    }
1993
    v9fs_post_create(s, vs, err);
1994
}
1995

    
1996
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1997
                                                                    int err)
1998
{
1999
    if (!vs->fidp->dir) {
2000
        err = -errno;
2001
    }
2002
    v9fs_post_create(s, vs, err);
2003
}
2004

    
2005
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2006
                                                                    int err)
2007
{
2008
    if (err) {
2009
        err = -errno;
2010
        goto out;
2011
    }
2012

    
2013
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
2014
    v9fs_create_post_opendir(s, vs, err);
2015
    return;
2016

    
2017
out:
2018
    v9fs_post_create(s, vs, err);
2019
}
2020

    
2021
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2022
{
2023
    if (err) {
2024
        err = -errno;
2025
        goto out;
2026
    }
2027

    
2028
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2029
    v9fs_create_post_dir_lstat(s, vs, err);
2030
    return;
2031

    
2032
out:
2033
    v9fs_post_create(s, vs, err);
2034
}
2035

    
2036
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2037
{
2038
    if (err) {
2039
        vs->fidp->fd = -1;
2040
        err = -errno;
2041
    }
2042

    
2043
    v9fs_post_create(s, vs, err);
2044
    return;
2045
}
2046

    
2047
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2048
{
2049
    if (vs->fidp->fd == -1) {
2050
        err = -errno;
2051
        goto out;
2052
    }
2053

    
2054
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
2055
    v9fs_create_post_fstat(s, vs, err);
2056

    
2057
    return;
2058

    
2059
out:
2060
    v9fs_post_create(s, vs, err);
2061

    
2062
}
2063

    
2064
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2065
{
2066

    
2067
    if (err == 0 || errno != ENOENT) {
2068
        err = -errno;
2069
        goto out;
2070
    }
2071

    
2072
    if (vs->perm & P9_STAT_MODE_DIR) {
2073
        err = v9fs_do_mkdir(s, vs);
2074
        v9fs_create_post_mkdir(s, vs, err);
2075
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2076
        err = v9fs_do_symlink(s, vs);
2077
        v9fs_create_post_perms(s, vs, err);
2078
    } else if (vs->perm & P9_STAT_MODE_LINK) {
2079
        int32_t nfid = atoi(vs->extension.data);
2080
        V9fsFidState *nfidp = lookup_fid(s, nfid);
2081
        if (nfidp == NULL) {
2082
            err = -errno;
2083
            v9fs_post_create(s, vs, err);
2084
        }
2085
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2086
        v9fs_create_post_perms(s, vs, err);
2087
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2088
        char ctype;
2089
        uint32_t major, minor;
2090
        mode_t nmode = 0;
2091

    
2092
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2093
                                        &minor) != 3) {
2094
            err = -errno;
2095
            v9fs_post_create(s, vs, err);
2096
        }
2097

    
2098
        switch (ctype) {
2099
        case 'c':
2100
            nmode = S_IFCHR;
2101
            break;
2102
        case 'b':
2103
            nmode = S_IFBLK;
2104
            break;
2105
        default:
2106
            err = -EIO;
2107
            v9fs_post_create(s, vs, err);
2108
        }
2109

    
2110
        nmode |= vs->perm & 0777;
2111
        err = v9fs_do_mknod(s, vs, nmode, makedev(major, minor));
2112
        v9fs_create_post_perms(s, vs, err);
2113
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2114
        err = v9fs_do_mknod(s, vs, S_IFIFO | (vs->perm & 0777), 0);
2115
        v9fs_post_create(s, vs, err);
2116
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2117
        err = v9fs_do_mknod(s, vs, S_IFSOCK | (vs->perm & 0777), 0);
2118
        v9fs_post_create(s, vs, err);
2119
    } else {
2120
        vs->fidp->fd = v9fs_do_open2(s, vs);
2121
        v9fs_create_post_open2(s, vs, err);
2122
    }
2123

    
2124
    return;
2125

    
2126
out:
2127
    v9fs_post_create(s, vs, err);
2128
}
2129

    
2130
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2131
{
2132
    int32_t fid;
2133
    V9fsCreateState *vs;
2134
    int err = 0;
2135

    
2136
    vs = qemu_malloc(sizeof(*vs));
2137
    vs->pdu = pdu;
2138
    vs->offset = 7;
2139

    
2140
    v9fs_string_init(&vs->fullname);
2141

    
2142
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2143
                                &vs->perm, &vs->mode, &vs->extension);
2144

    
2145
    vs->fidp = lookup_fid(s, fid);
2146
    if (vs->fidp == NULL) {
2147
        err = -EINVAL;
2148
        goto out;
2149
    }
2150

    
2151
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2152
                                                        vs->name.data);
2153

    
2154
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2155
    v9fs_create_post_lstat(s, vs, err);
2156
    return;
2157

    
2158
out:
2159
    complete_pdu(s, vs->pdu, err);
2160
    v9fs_string_free(&vs->name);
2161
    v9fs_string_free(&vs->extension);
2162
    qemu_free(vs);
2163
}
2164

    
2165
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2166
{
2167
    /* A nop call with no return */
2168
    complete_pdu(s, pdu, 7);
2169
}
2170

    
2171
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2172
                                                                int err)
2173
{
2174
    if (err < 0) {
2175
        err = -errno;
2176
    } else {
2177
        err = vs->offset;
2178
    }
2179

    
2180
    /* For TREMOVE we need to clunk the fid even on failed remove */
2181
    free_fid(s, vs->fidp->fid);
2182

    
2183
    complete_pdu(s, vs->pdu, err);
2184
    qemu_free(vs);
2185
}
2186

    
2187
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2188
{
2189
    int32_t fid;
2190
    V9fsRemoveState *vs;
2191
    int err = 0;
2192

    
2193
    vs = qemu_malloc(sizeof(*vs));
2194
    vs->pdu = pdu;
2195
    vs->offset = 7;
2196

    
2197
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2198

    
2199
    vs->fidp = lookup_fid(s, fid);
2200
    if (vs->fidp == NULL) {
2201
        err = -EINVAL;
2202
        goto out;
2203
    }
2204

    
2205
    err = v9fs_do_remove(s, &vs->fidp->path);
2206
    v9fs_remove_post_remove(s, vs, err);
2207
    return;
2208

    
2209
out:
2210
    complete_pdu(s, pdu, err);
2211
    qemu_free(vs);
2212
}
2213

    
2214
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2215
{
2216
    if (err < 0) {
2217
        goto out;
2218
    }
2219

    
2220
    err = vs->offset;
2221

    
2222
out:
2223
    v9fs_stat_free(&vs->v9stat);
2224
    complete_pdu(s, vs->pdu, err);
2225
    qemu_free(vs);
2226
}
2227

    
2228
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2229
{
2230
    if (err < 0) {
2231
        goto out;
2232
    }
2233

    
2234
    if (vs->v9stat.name.size != 0) {
2235
        v9fs_string_free(&vs->nname);
2236
    }
2237

    
2238
    if (vs->v9stat.length != -1) {
2239
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2240
            err = -errno;
2241
        }
2242
    }
2243
    v9fs_wstat_post_truncate(s, vs, err);
2244
    return;
2245

    
2246
out:
2247
    v9fs_stat_free(&vs->v9stat);
2248
    complete_pdu(s, vs->pdu, err);
2249
    qemu_free(vs);
2250
}
2251

    
2252
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2253
{
2254
    V9fsFidState *fidp;
2255
    if (err < 0) {
2256
        goto out;
2257
    }
2258

    
2259
    if (vs->v9stat.name.size != 0) {
2260
        char *old_name, *new_name;
2261
        char *end;
2262

    
2263
        old_name = vs->fidp->path.data;
2264
        end = strrchr(old_name, '/');
2265
        if (end) {
2266
            end++;
2267
        } else {
2268
            end = old_name;
2269
        }
2270

    
2271
        new_name = qemu_mallocz(end - old_name + vs->v9stat.name.size + 1);
2272

    
2273
        memcpy(new_name, old_name, end - old_name);
2274
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
2275
                vs->v9stat.name.size);
2276
        vs->nname.data = new_name;
2277
        vs->nname.size = strlen(new_name);
2278

    
2279
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
2280
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2281
                err = -errno;
2282
            } else {
2283
                /*
2284
                 * Fixup fid's pointing to the old name to
2285
                 * start pointing to the new name
2286
                 */
2287
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2288

    
2289
                    if (vs->fidp == fidp) {
2290
                        /*
2291
                         * we replace name of this fid towards the end
2292
                         * so that our below strcmp will work
2293
                         */
2294
                        continue;
2295
                    }
2296
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
2297
                                 strlen(vs->fidp->path.data))) {
2298
                        /* replace the name */
2299
                        v9fs_fix_path(&fidp->path, &vs->nname,
2300
                                      strlen(vs->fidp->path.data));
2301
                    }
2302
                }
2303
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2304
            }
2305
        }
2306
    }
2307
    v9fs_wstat_post_rename(s, vs, err);
2308
    return;
2309

    
2310
out:
2311
    v9fs_stat_free(&vs->v9stat);
2312
    complete_pdu(s, vs->pdu, err);
2313
    qemu_free(vs);
2314
}
2315

    
2316
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2317
{
2318
    if (err < 0) {
2319
        goto out;
2320
    }
2321

    
2322
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2323
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2324
                    vs->v9stat.n_gid)) {
2325
            err = -errno;
2326
        }
2327
    }
2328
    v9fs_wstat_post_chown(s, vs, err);
2329
    return;
2330

    
2331
out:
2332
    v9fs_stat_free(&vs->v9stat);
2333
    complete_pdu(s, vs->pdu, err);
2334
    qemu_free(vs);
2335
}
2336

    
2337
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2338
{
2339
    if (err < 0) {
2340
        goto out;
2341
    }
2342

    
2343
    if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2344
        if (v9fs_do_utimensat(s, &vs->fidp->path, vs->v9stat)) {
2345
            err = -errno;
2346
        }
2347
    }
2348

    
2349
    v9fs_wstat_post_utime(s, vs, err);
2350
    return;
2351

    
2352
out:
2353
    v9fs_stat_free(&vs->v9stat);
2354
    complete_pdu(s, vs->pdu, err);
2355
    qemu_free(vs);
2356
}
2357

    
2358
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2359
{
2360
    if (err == -1) {
2361
        err = -errno;
2362
    }
2363
    v9fs_stat_free(&vs->v9stat);
2364
    complete_pdu(s, vs->pdu, err);
2365
    qemu_free(vs);
2366
}
2367

    
2368
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2369
{
2370
    uint32_t v9_mode;
2371

    
2372
    if (err == -1) {
2373
        err = -errno;
2374
        goto out;
2375
    }
2376

    
2377
    v9_mode = stat_to_v9mode(&vs->stbuf);
2378

    
2379
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2380
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2381
            /* Attempting to change the type */
2382
            err = -EIO;
2383
            goto out;
2384
    }
2385

    
2386
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2387
                    &vs->v9stat.extension))) {
2388
            err = -errno;
2389
     }
2390
    v9fs_wstat_post_chmod(s, vs, err);
2391
    return;
2392

    
2393
out:
2394
    v9fs_stat_free(&vs->v9stat);
2395
    complete_pdu(s, vs->pdu, err);
2396
    qemu_free(vs);
2397
}
2398

    
2399
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2400
{
2401
    int32_t fid;
2402
    V9fsWstatState *vs;
2403
    int err = 0;
2404

    
2405
    vs = qemu_malloc(sizeof(*vs));
2406
    vs->pdu = pdu;
2407
    vs->offset = 7;
2408

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

    
2411
    vs->fidp = lookup_fid(s, fid);
2412
    if (vs->fidp == NULL) {
2413
        err = -EINVAL;
2414
        goto out;
2415
    }
2416

    
2417
    /* do we need to sync the file? */
2418
    if (donttouch_stat(&vs->v9stat)) {
2419
        err = v9fs_do_fsync(s, vs->fidp->fd);
2420
        v9fs_wstat_post_fsync(s, vs, err);
2421
        return;
2422
    }
2423

    
2424
    if (vs->v9stat.mode != -1) {
2425
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2426
        v9fs_wstat_post_lstat(s, vs, err);
2427
        return;
2428
    }
2429

    
2430
    v9fs_wstat_post_chmod(s, vs, err);
2431
    return;
2432

    
2433
out:
2434
    v9fs_stat_free(&vs->v9stat);
2435
    complete_pdu(s, vs->pdu, err);
2436
    qemu_free(vs);
2437
}
2438

    
2439
static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
2440
{
2441
    int32_t bsize_factor;
2442

    
2443
    if (err) {
2444
        err = -errno;
2445
        goto out;
2446
    }
2447

    
2448
    /*
2449
     * compute bsize factor based on host file system block size
2450
     * and client msize
2451
     */
2452
    bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
2453
    if (!bsize_factor) {
2454
        bsize_factor = 1;
2455
    }
2456
    vs->v9statfs.f_type = vs->stbuf.f_type;
2457
    vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
2458
    vs->v9statfs.f_bsize *= bsize_factor;
2459
    /*
2460
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2461
     * adjust(divide) the number of blocks, free blocks and available
2462
     * blocks by bsize factor
2463
     */
2464
    vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
2465
    vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
2466
    vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
2467
    vs->v9statfs.f_files = vs->stbuf.f_files;
2468
    vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
2469
    vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
2470
                        (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
2471
    vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
2472

    
2473
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
2474
         vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
2475
         vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
2476
         vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
2477
         vs->v9statfs.f_namelen);
2478

    
2479
out:
2480
    complete_pdu(s, vs->pdu, vs->offset);
2481
    qemu_free(vs);
2482
}
2483

    
2484
static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
2485
{
2486
    V9fsStatfsState *vs;
2487
    ssize_t err = 0;
2488

    
2489
    vs = qemu_malloc(sizeof(*vs));
2490
    vs->pdu = pdu;
2491
    vs->offset = 7;
2492

    
2493
    memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
2494

    
2495
    pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
2496

    
2497
    vs->fidp = lookup_fid(s, vs->fid);
2498
    if (vs->fidp == NULL) {
2499
        err = -ENOENT;
2500
        goto out;
2501
    }
2502

    
2503
    err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
2504
    v9fs_statfs_post_statfs(s, vs, err);
2505
    return;
2506

    
2507
out:
2508
    complete_pdu(s, vs->pdu, err);
2509
    qemu_free(vs);
2510
}
2511

    
2512
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2513

    
2514
static pdu_handler_t *pdu_handlers[] = {
2515
    [P9_TREADDIR] = v9fs_readdir,
2516
    [P9_TSTATFS] = v9fs_statfs,
2517
    [P9_TGETATTR] = v9fs_getattr,
2518
    [P9_TVERSION] = v9fs_version,
2519
    [P9_TATTACH] = v9fs_attach,
2520
    [P9_TSTAT] = v9fs_stat,
2521
    [P9_TWALK] = v9fs_walk,
2522
    [P9_TCLUNK] = v9fs_clunk,
2523
    [P9_TOPEN] = v9fs_open,
2524
    [P9_TREAD] = v9fs_read,
2525
#if 0
2526
    [P9_TAUTH] = v9fs_auth,
2527
#endif
2528
    [P9_TFLUSH] = v9fs_flush,
2529
    [P9_TCREATE] = v9fs_create,
2530
    [P9_TWRITE] = v9fs_write,
2531
    [P9_TWSTAT] = v9fs_wstat,
2532
    [P9_TREMOVE] = v9fs_remove,
2533
};
2534

    
2535
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2536
{
2537
    pdu_handler_t *handler;
2538

    
2539
    if (debug_9p_pdu) {
2540
        pprint_pdu(pdu);
2541
    }
2542

    
2543
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2544

    
2545
    handler = pdu_handlers[pdu->id];
2546
    BUG_ON(handler == NULL);
2547

    
2548
    handler(s, pdu);
2549
}
2550

    
2551
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2552
{
2553
    V9fsState *s = (V9fsState *)vdev;
2554
    V9fsPDU *pdu;
2555
    ssize_t len;
2556

    
2557
    while ((pdu = alloc_pdu(s)) &&
2558
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2559
        uint8_t *ptr;
2560

    
2561
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2562
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2563

    
2564
        ptr = pdu->elem.out_sg[0].iov_base;
2565

    
2566
        memcpy(&pdu->size, ptr, 4);
2567
        pdu->id = ptr[4];
2568
        memcpy(&pdu->tag, ptr + 5, 2);
2569

    
2570
        submit_pdu(s, pdu);
2571
    }
2572

    
2573
    free_pdu(s, pdu);
2574
}
2575

    
2576
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2577
{
2578
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2579
    return features;
2580
}
2581

    
2582
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2583
{
2584
    return (V9fsState *)vdev;
2585
}
2586

    
2587
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2588
{
2589
    struct virtio_9p_config *cfg;
2590
    V9fsState *s = to_virtio_9p(vdev);
2591

    
2592
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2593
                        s->tag_len);
2594
    stw_raw(&cfg->tag_len, s->tag_len);
2595
    memcpy(cfg->tag, s->tag, s->tag_len);
2596
    memcpy(config, cfg, s->config_size);
2597
    qemu_free(cfg);
2598
}
2599

    
2600
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2601
 {
2602
    V9fsState *s;
2603
    int i, len;
2604
    struct stat stat;
2605
    FsTypeEntry *fse;
2606

    
2607

    
2608
    s = (V9fsState *)virtio_common_init("virtio-9p",
2609
                                    VIRTIO_ID_9P,
2610
                                    sizeof(struct virtio_9p_config)+
2611
                                    MAX_TAG_LEN,
2612
                                    sizeof(V9fsState));
2613

    
2614
    /* initialize pdu allocator */
2615
    QLIST_INIT(&s->free_list);
2616
    for (i = 0; i < (MAX_REQ - 1); i++) {
2617
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2618
    }
2619

    
2620
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2621

    
2622
    fse = get_fsdev_fsentry(conf->fsdev_id);
2623

    
2624
    if (!fse) {
2625
        /* We don't have a fsdev identified by fsdev_id */
2626
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2627
                    "with the id %s\n", conf->fsdev_id);
2628
        exit(1);
2629
    }
2630

    
2631
    if (!fse->path || !conf->tag) {
2632
        /* we haven't specified a mount_tag or the path */
2633
        fprintf(stderr, "fsdev with id %s needs path "
2634
                "and Virtio-9p device needs mount_tag arguments\n",
2635
                conf->fsdev_id);
2636
        exit(1);
2637
    }
2638

    
2639
    if (!strcmp(fse->security_model, "passthrough")) {
2640
        /* Files on the Fileserver set to client user credentials */
2641
        s->ctx.fs_sm = SM_PASSTHROUGH;
2642
    } else if (!strcmp(fse->security_model, "mapped")) {
2643
        /* Files on the fileserver are set to QEMU credentials.
2644
         * Client user credentials are saved in extended attributes.
2645
         */
2646
        s->ctx.fs_sm = SM_MAPPED;
2647
    } else {
2648
        /* user haven't specified a correct security option */
2649
        fprintf(stderr, "one of the following must be specified as the"
2650
                "security option:\n\t security_model=passthrough \n\t "
2651
                "security_model=mapped\n");
2652
        return NULL;
2653
    }
2654

    
2655
    if (lstat(fse->path, &stat)) {
2656
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2657
        exit(1);
2658
    } else if (!S_ISDIR(stat.st_mode)) {
2659
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2660
        exit(1);
2661
    }
2662

    
2663
    s->ctx.fs_root = qemu_strdup(fse->path);
2664
    len = strlen(conf->tag);
2665
    if (len > MAX_TAG_LEN) {
2666
        len = MAX_TAG_LEN;
2667
    }
2668
    /* s->tag is non-NULL terminated string */
2669
    s->tag = qemu_malloc(len);
2670
    memcpy(s->tag, conf->tag, len);
2671
    s->tag_len = len;
2672
    s->ctx.uid = -1;
2673

    
2674
    s->ops = fse->ops;
2675
    s->vdev.get_features = virtio_9p_get_features;
2676
    s->config_size = sizeof(struct virtio_9p_config) +
2677
                        s->tag_len;
2678
    s->vdev.get_config = virtio_9p_get_config;
2679

    
2680
    return &s->vdev;
2681
}