Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (89.6 kB)

1
/*
2
 * Virtio 9p backend
3
 *
4
 * Copyright IBM, Corp. 2010
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13

    
14
#include "virtio.h"
15
#include "pc.h"
16
#include "qemu_socket.h"
17
#include "virtio-9p.h"
18
#include "fsdev/qemu-fsdev.h"
19
#include "virtio-9p-debug.h"
20

    
21
int debug_9p_pdu;
22

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

    
35
static int omode_to_uflags(int8_t mode)
36
{
37
    int ret = 0;
38

    
39
    switch (mode & 3) {
40
    case Oread:
41
        ret = O_RDONLY;
42
        break;
43
    case Ordwr:
44
        ret = O_RDWR;
45
        break;
46
    case Owrite:
47
        ret = O_WRONLY;
48
        break;
49
    case Oexec:
50
        ret = O_RDONLY;
51
        break;
52
    }
53

    
54
    if (mode & Otrunc) {
55
        ret |= O_TRUNC;
56
    }
57

    
58
    if (mode & Oappend) {
59
        ret |= O_APPEND;
60
    }
61

    
62
    if (mode & Oexcl) {
63
        ret |= O_EXCL;
64
    }
65

    
66
    return ret;
67
}
68

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

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

    
82
static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
83
{
84
    ssize_t len;
85

    
86
    buf->data = qemu_malloc(1024);
87

    
88
    len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
89
    if (len > -1) {
90
        buf->size = len;
91
        buf->data[len] = 0;
92
    }
93

    
94
    return len;
95
}
96

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

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

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

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

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

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

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

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

    
137
static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
138
                            int iovcnt)
139
{
140
    return s->ops->readv(&s->ctx, fd, iov, iovcnt);
141
}
142

    
143
static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
144
{
145
    return s->ops->lseek(&s->ctx, fd, offset, whence);
146
}
147

    
148
static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
149
                       int iovcnt)
150
{
151
    return s->ops->writev(&s->ctx, fd, iov, iovcnt);
152
}
153

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

    
162
static int v9fs_do_mknod(V9fsState *s, char *name,
163
        mode_t mode, dev_t dev, uid_t uid, gid_t gid)
164
{
165
    FsCred cred;
166
    cred_init(&cred);
167
    cred.fc_uid = uid;
168
    cred.fc_gid = gid;
169
    cred.fc_mode = mode;
170
    cred.fc_rdev = dev;
171
    return s->ops->mknod(&s->ctx, name, &cred);
172
}
173

    
174
static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
175
                uid_t uid, gid_t gid)
176
{
177
    FsCred cred;
178

    
179
    cred_init(&cred);
180
    cred.fc_uid = uid;
181
    cred.fc_gid = gid;
182
    cred.fc_mode = mode;
183

    
184
    return s->ops->mkdir(&s->ctx, name, &cred);
185
}
186

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

    
192
static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
193
        int flags, int mode)
194
{
195
    FsCred cred;
196

    
197
    cred_init(&cred);
198
    cred.fc_uid = uid;
199
    cred.fc_gid = gid;
200
    cred.fc_mode = mode & 07777;
201
    flags = flags;
202

    
203
    return s->ops->open2(&s->ctx, fullname, flags, &cred);
204
}
205

    
206
static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
207
        const char *oldpath, const char *newpath, gid_t gid)
208
{
209
    FsCred cred;
210
    cred_init(&cred);
211
    cred.fc_uid = fidp->uid;
212
    cred.fc_gid = gid;
213
    cred.fc_mode = 0777;
214

    
215
    return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
216
}
217

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

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

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

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

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

    
244
static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
245
                                           const struct timespec times[2])
246
{
247
    return s->ops->utimensat(&s->ctx, path->data, times);
248
}
249

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

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

    
260
static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
261
{
262
    return s->ops->statfs(&s->ctx, path->data, stbuf);
263
}
264

    
265
static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
266
                             V9fsString *xattr_name,
267
                             void *value, size_t size)
268
{
269
    return s->ops->lgetxattr(&s->ctx, path->data,
270
                             xattr_name->data, value, size);
271
}
272

    
273
static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
274
                              void *value, size_t size)
275
{
276
    return s->ops->llistxattr(&s->ctx, path->data,
277
                              value, size);
278
}
279

    
280
static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
281
                             V9fsString *xattr_name,
282
                             void *value, size_t size, int flags)
283
{
284
    return s->ops->lsetxattr(&s->ctx, path->data,
285
                             xattr_name->data, value, size, flags);
286
}
287

    
288
static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
289
                                V9fsString *xattr_name)
290
{
291
    return s->ops->lremovexattr(&s->ctx, path->data,
292
                                xattr_name->data);
293
}
294

    
295

    
296
static void v9fs_string_init(V9fsString *str)
297
{
298
    str->data = NULL;
299
    str->size = 0;
300
}
301

    
302
static void v9fs_string_free(V9fsString *str)
303
{
304
    qemu_free(str->data);
305
    str->data = NULL;
306
    str->size = 0;
307
}
308

    
309
static void v9fs_string_null(V9fsString *str)
310
{
311
    v9fs_string_free(str);
312
}
313

    
314
static int number_to_string(void *arg, char type)
315
{
316
    unsigned int ret = 0;
317

    
318
    switch (type) {
319
    case 'u': {
320
        unsigned int num = *(unsigned int *)arg;
321

    
322
        do {
323
            ret++;
324
            num = num/10;
325
        } while (num);
326
        break;
327
    }
328
    default:
329
        printf("Number_to_string: Unknown number format\n");
330
        return -1;
331
    }
332

    
333
    return ret;
334
}
335

    
336
static int GCC_FMT_ATTR(2, 0)
337
v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
338
{
339
    va_list ap2;
340
    char *iter = (char *)fmt;
341
    int len = 0;
342
    int nr_args = 0;
343
    char *arg_char_ptr;
344
    unsigned int arg_uint;
345

    
346
    /* Find the number of %'s that denotes an argument */
347
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
348
        nr_args++;
349
        iter++;
350
    }
351

    
352
    len = strlen(fmt) - 2*nr_args;
353

    
354
    if (!nr_args) {
355
        goto alloc_print;
356
    }
357

    
358
    va_copy(ap2, ap);
359

    
360
    iter = (char *)fmt;
361

    
362
    /* Now parse the format string */
363
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
364
        iter++;
365
        switch (*iter) {
366
        case 'u':
367
            arg_uint = va_arg(ap2, unsigned int);
368
            len += number_to_string((void *)&arg_uint, 'u');
369
            break;
370
        case 's':
371
            arg_char_ptr = va_arg(ap2, char *);
372
            len += strlen(arg_char_ptr);
373
            break;
374
        case 'c':
375
            len += 1;
376
            break;
377
        default:
378
            fprintf(stderr,
379
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
380
            return -1;
381
        }
382
        iter++;
383
    }
384

    
385
alloc_print:
386
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
387

    
388
    return vsprintf(*strp, fmt, ap);
389
}
390

    
391
static void GCC_FMT_ATTR(2, 3)
392
v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
393
{
394
    va_list ap;
395
    int err;
396

    
397
    v9fs_string_free(str);
398

    
399
    va_start(ap, fmt);
400
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
401
    BUG_ON(err == -1);
402
    va_end(ap);
403

    
404
    str->size = err;
405
}
406

    
407
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
408
{
409
    v9fs_string_free(lhs);
410
    v9fs_string_sprintf(lhs, "%s", rhs->data);
411
}
412

    
413
static size_t v9fs_string_size(V9fsString *str)
414
{
415
    return str->size;
416
}
417

    
418
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
419
{
420
    V9fsFidState *f;
421

    
422
    for (f = s->fid_list; f; f = f->next) {
423
        if (f->fid == fid) {
424
            return f;
425
        }
426
    }
427

    
428
    return NULL;
429
}
430

    
431
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
432
{
433
    V9fsFidState *f;
434

    
435
    f = lookup_fid(s, fid);
436
    if (f) {
437
        return NULL;
438
    }
439

    
440
    f = qemu_mallocz(sizeof(V9fsFidState));
441

    
442
    f->fid = fid;
443
    f->fid_type = P9_FID_NONE;
444

    
445
    f->next = s->fid_list;
446
    s->fid_list = f;
447

    
448
    return f;
449
}
450

    
451
static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
452
{
453
    int retval = 0;
454

    
455
    if (fidp->fs.xattr.copied_len == -1) {
456
        /* getxattr/listxattr fid */
457
        goto free_value;
458
    }
459
    /*
460
     * if this is fid for setxattr. clunk should
461
     * result in setxattr localcall
462
     */
463
    if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
464
        /* clunk after partial write */
465
        retval = -EINVAL;
466
        goto free_out;
467
    }
468
    if (fidp->fs.xattr.len) {
469
        retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
470
                                   fidp->fs.xattr.value,
471
                                   fidp->fs.xattr.len,
472
                                   fidp->fs.xattr.flags);
473
    } else {
474
        retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
475
    }
476
free_out:
477
    v9fs_string_free(&fidp->fs.xattr.name);
478
free_value:
479
    if (fidp->fs.xattr.value) {
480
        qemu_free(fidp->fs.xattr.value);
481
    }
482
    return retval;
483
}
484

    
485
static int free_fid(V9fsState *s, int32_t fid)
486
{
487
    int retval = 0;
488
    V9fsFidState **fidpp, *fidp;
489

    
490
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
491
        if ((*fidpp)->fid == fid) {
492
            break;
493
        }
494
    }
495

    
496
    if (*fidpp == NULL) {
497
        return -ENOENT;
498
    }
499

    
500
    fidp = *fidpp;
501
    *fidpp = fidp->next;
502

    
503
    if (fidp->fid_type == P9_FID_FILE) {
504
        v9fs_do_close(s, fidp->fs.fd);
505
    } else if (fidp->fid_type == P9_FID_DIR) {
506
        v9fs_do_closedir(s, fidp->fs.dir);
507
    } else if (fidp->fid_type == P9_FID_XATTR) {
508
        retval = v9fs_xattr_fid_clunk(s, fidp);
509
    }
510
    v9fs_string_free(&fidp->path);
511
    qemu_free(fidp);
512

    
513
    return retval;
514
}
515

    
516
#define P9_QID_TYPE_DIR         0x80
517
#define P9_QID_TYPE_SYMLINK     0x02
518

    
519
#define P9_STAT_MODE_DIR        0x80000000
520
#define P9_STAT_MODE_APPEND     0x40000000
521
#define P9_STAT_MODE_EXCL       0x20000000
522
#define P9_STAT_MODE_MOUNT      0x10000000
523
#define P9_STAT_MODE_AUTH       0x08000000
524
#define P9_STAT_MODE_TMP        0x04000000
525
#define P9_STAT_MODE_SYMLINK    0x02000000
526
#define P9_STAT_MODE_LINK       0x01000000
527
#define P9_STAT_MODE_DEVICE     0x00800000
528
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
529
#define P9_STAT_MODE_SOCKET     0x00100000
530
#define P9_STAT_MODE_SETUID     0x00080000
531
#define P9_STAT_MODE_SETGID     0x00040000
532
#define P9_STAT_MODE_SETVTX     0x00010000
533

    
534
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
535
                                P9_STAT_MODE_SYMLINK |      \
536
                                P9_STAT_MODE_LINK |         \
537
                                P9_STAT_MODE_DEVICE |       \
538
                                P9_STAT_MODE_NAMED_PIPE |   \
539
                                P9_STAT_MODE_SOCKET)
540

    
541
/* This is the algorithm from ufs in spfs */
542
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
543
{
544
    size_t size;
545

    
546
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
547
    memcpy(&qidp->path, &stbuf->st_ino, size);
548
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
549
    qidp->type = 0;
550
    if (S_ISDIR(stbuf->st_mode)) {
551
        qidp->type |= P9_QID_TYPE_DIR;
552
    }
553
    if (S_ISLNK(stbuf->st_mode)) {
554
        qidp->type |= P9_QID_TYPE_SYMLINK;
555
    }
556
}
557

    
558
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
559
{
560
    struct stat stbuf;
561
    int err;
562

    
563
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
564
    if (err) {
565
        return err;
566
    }
567

    
568
    stat_to_qid(&stbuf, qidp);
569
    return 0;
570
}
571

    
572
static V9fsPDU *alloc_pdu(V9fsState *s)
573
{
574
    V9fsPDU *pdu = NULL;
575

    
576
    if (!QLIST_EMPTY(&s->free_list)) {
577
        pdu = QLIST_FIRST(&s->free_list);
578
        QLIST_REMOVE(pdu, next);
579
    }
580
    return pdu;
581
}
582

    
583
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
584
{
585
    if (pdu) {
586
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
587
    }
588
}
589

    
590
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
591
                        size_t offset, size_t size, int pack)
592
{
593
    int i = 0;
594
    size_t copied = 0;
595

    
596
    for (i = 0; size && i < sg_count; i++) {
597
        size_t len;
598
        if (offset >= sg[i].iov_len) {
599
            /* skip this sg */
600
            offset -= sg[i].iov_len;
601
            continue;
602
        } else {
603
            len = MIN(sg[i].iov_len - offset, size);
604
            if (pack) {
605
                memcpy(sg[i].iov_base + offset, addr, len);
606
            } else {
607
                memcpy(addr, sg[i].iov_base + offset, len);
608
            }
609
            size -= len;
610
            copied += len;
611
            addr += len;
612
            if (size) {
613
                offset = 0;
614
                continue;
615
            }
616
        }
617
    }
618

    
619
    return copied;
620
}
621

    
622
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
623
{
624
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
625
                         offset, size, 0);
626
}
627

    
628
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
629
                        size_t size)
630
{
631
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
632
                             offset, size, 1);
633
}
634

    
635
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
636
{
637
    size_t pos = 0;
638
    int i, j;
639
    struct iovec *src_sg;
640
    unsigned int num;
641

    
642
    if (rx) {
643
        src_sg = pdu->elem.in_sg;
644
        num = pdu->elem.in_num;
645
    } else {
646
        src_sg = pdu->elem.out_sg;
647
        num = pdu->elem.out_num;
648
    }
649

    
650
    j = 0;
651
    for (i = 0; i < num; i++) {
652
        if (offset <= pos) {
653
            sg[j].iov_base = src_sg[i].iov_base;
654
            sg[j].iov_len = src_sg[i].iov_len;
655
            j++;
656
        } else if (offset < (src_sg[i].iov_len + pos)) {
657
            sg[j].iov_base = src_sg[i].iov_base;
658
            sg[j].iov_len = src_sg[i].iov_len;
659
            sg[j].iov_base += (offset - pos);
660
            sg[j].iov_len -= (offset - pos);
661
            j++;
662
        }
663
        pos += src_sg[i].iov_len;
664
    }
665

    
666
    return j;
667
}
668

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

    
675
    va_start(ap, fmt);
676
    for (i = 0; fmt[i]; i++) {
677
        switch (fmt[i]) {
678
        case 'b': {
679
            uint8_t *valp = va_arg(ap, uint8_t *);
680
            offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
681
            break;
682
        }
683
        case 'w': {
684
            uint16_t val, *valp;
685
            valp = va_arg(ap, uint16_t *);
686
            val = le16_to_cpupu(valp);
687
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
688
            *valp = val;
689
            break;
690
        }
691
        case 'd': {
692
            uint32_t val, *valp;
693
            valp = va_arg(ap, uint32_t *);
694
            val = le32_to_cpupu(valp);
695
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
696
            *valp = val;
697
            break;
698
        }
699
        case 'q': {
700
            uint64_t val, *valp;
701
            valp = va_arg(ap, uint64_t *);
702
            val = le64_to_cpup(valp);
703
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
704
            *valp = val;
705
            break;
706
        }
707
        case 'v': {
708
            struct iovec *iov = va_arg(ap, struct iovec *);
709
            int *iovcnt = va_arg(ap, int *);
710
            *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
711
            break;
712
        }
713
        case 's': {
714
            V9fsString *str = va_arg(ap, V9fsString *);
715
            offset += pdu_unmarshal(pdu, offset, "w", &str->size);
716
            /* FIXME: sanity check str->size */
717
            str->data = qemu_malloc(str->size + 1);
718
            offset += pdu_unpack(str->data, pdu, offset, str->size);
719
            str->data[str->size] = 0;
720
            break;
721
        }
722
        case 'Q': {
723
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
724
            offset += pdu_unmarshal(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_unmarshal(pdu, offset, "wwdQdddqsssssddd",
731
                        &statp->size, &statp->type, &statp->dev,
732
                        &statp->qid, &statp->mode, &statp->atime,
733
                        &statp->mtime, &statp->length,
734
                        &statp->name, &statp->uid, &statp->gid,
735
                        &statp->muid, &statp->extension,
736
                        &statp->n_uid, &statp->n_gid,
737
                        &statp->n_muid);
738
            break;
739
        }
740
        case 'I': {
741
            V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
742
            offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
743
                        &iattr->valid, &iattr->mode,
744
                        &iattr->uid, &iattr->gid, &iattr->size,
745
                        &iattr->atime_sec, &iattr->atime_nsec,
746
                        &iattr->mtime_sec, &iattr->mtime_nsec);
747
            break;
748
        }
749
        default:
750
            break;
751
        }
752
    }
753

    
754
    va_end(ap);
755

    
756
    return offset - old_offset;
757
}
758

    
759
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
760
{
761
    size_t old_offset = offset;
762
    va_list ap;
763
    int i;
764

    
765
    va_start(ap, fmt);
766
    for (i = 0; fmt[i]; i++) {
767
        switch (fmt[i]) {
768
        case 'b': {
769
            uint8_t val = va_arg(ap, int);
770
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
771
            break;
772
        }
773
        case 'w': {
774
            uint16_t val;
775
            cpu_to_le16w(&val, va_arg(ap, int));
776
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
777
            break;
778
        }
779
        case 'd': {
780
            uint32_t val;
781
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
782
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
783
            break;
784
        }
785
        case 'q': {
786
            uint64_t val;
787
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
788
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
789
            break;
790
        }
791
        case 'v': {
792
            struct iovec *iov = va_arg(ap, struct iovec *);
793
            int *iovcnt = va_arg(ap, int *);
794
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
795
            break;
796
        }
797
        case 's': {
798
            V9fsString *str = va_arg(ap, V9fsString *);
799
            offset += pdu_marshal(pdu, offset, "w", str->size);
800
            offset += pdu_pack(pdu, offset, str->data, str->size);
801
            break;
802
        }
803
        case 'Q': {
804
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
805
            offset += pdu_marshal(pdu, offset, "bdq",
806
                        qidp->type, qidp->version, qidp->path);
807
            break;
808
        }
809
        case 'S': {
810
            V9fsStat *statp = va_arg(ap, V9fsStat *);
811
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
812
                        statp->size, statp->type, statp->dev,
813
                        &statp->qid, statp->mode, statp->atime,
814
                        statp->mtime, statp->length, &statp->name,
815
                        &statp->uid, &statp->gid, &statp->muid,
816
                        &statp->extension, statp->n_uid,
817
                        statp->n_gid, statp->n_muid);
818
            break;
819
        }
820
        case 'A': {
821
            V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
822
            offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
823
                        statp->st_result_mask,
824
                        &statp->qid, statp->st_mode,
825
                        statp->st_uid, statp->st_gid,
826
                        statp->st_nlink, statp->st_rdev,
827
                        statp->st_size, statp->st_blksize, statp->st_blocks,
828
                        statp->st_atime_sec, statp->st_atime_nsec,
829
                        statp->st_mtime_sec, statp->st_mtime_nsec,
830
                        statp->st_ctime_sec, statp->st_ctime_nsec,
831
                        statp->st_btime_sec, statp->st_btime_nsec,
832
                        statp->st_gen, statp->st_data_version);
833
            break;
834
        }
835
        default:
836
            break;
837
        }
838
    }
839
    va_end(ap);
840

    
841
    return offset - old_offset;
842
}
843

    
844
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
845
{
846
    int8_t id = pdu->id + 1; /* Response */
847

    
848
    if (len < 0) {
849
        int err = -len;
850
        len = 7;
851

    
852
        if (s->proto_version != V9FS_PROTO_2000L) {
853
            V9fsString str;
854

    
855
            str.data = strerror(err);
856
            str.size = strlen(str.data);
857

    
858
            len += pdu_marshal(pdu, len, "s", &str);
859
            id = P9_RERROR;
860
        }
861

    
862
        len += pdu_marshal(pdu, len, "d", err);
863

    
864
        if (s->proto_version == V9FS_PROTO_2000L) {
865
            id = P9_RLERROR;
866
        }
867
    }
868

    
869
    /* fill out the header */
870
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
871

    
872
    /* keep these in sync */
873
    pdu->size = len;
874
    pdu->id = id;
875

    
876
    /* push onto queue and notify */
877
    virtqueue_push(s->vq, &pdu->elem, len);
878

    
879
    /* FIXME: we should batch these completions */
880
    virtio_notify(&s->vdev, s->vq);
881

    
882
    free_pdu(s, pdu);
883
}
884

    
885
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
886
{
887
    mode_t ret;
888

    
889
    ret = mode & 0777;
890
    if (mode & P9_STAT_MODE_DIR) {
891
        ret |= S_IFDIR;
892
    }
893

    
894
    if (mode & P9_STAT_MODE_SYMLINK) {
895
        ret |= S_IFLNK;
896
    }
897
    if (mode & P9_STAT_MODE_SOCKET) {
898
        ret |= S_IFSOCK;
899
    }
900
    if (mode & P9_STAT_MODE_NAMED_PIPE) {
901
        ret |= S_IFIFO;
902
    }
903
    if (mode & P9_STAT_MODE_DEVICE) {
904
        if (extension && extension->data[0] == 'c') {
905
            ret |= S_IFCHR;
906
        } else {
907
            ret |= S_IFBLK;
908
        }
909
    }
910

    
911
    if (!(ret&~0777)) {
912
        ret |= S_IFREG;
913
    }
914

    
915
    if (mode & P9_STAT_MODE_SETUID) {
916
        ret |= S_ISUID;
917
    }
918
    if (mode & P9_STAT_MODE_SETGID) {
919
        ret |= S_ISGID;
920
    }
921
    if (mode & P9_STAT_MODE_SETVTX) {
922
        ret |= S_ISVTX;
923
    }
924

    
925
    return ret;
926
}
927

    
928
static int donttouch_stat(V9fsStat *stat)
929
{
930
    if (stat->type == -1 &&
931
        stat->dev == -1 &&
932
        stat->qid.type == -1 &&
933
        stat->qid.version == -1 &&
934
        stat->qid.path == -1 &&
935
        stat->mode == -1 &&
936
        stat->atime == -1 &&
937
        stat->mtime == -1 &&
938
        stat->length == -1 &&
939
        !stat->name.size &&
940
        !stat->uid.size &&
941
        !stat->gid.size &&
942
        !stat->muid.size &&
943
        stat->n_uid == -1 &&
944
        stat->n_gid == -1 &&
945
        stat->n_muid == -1) {
946
        return 1;
947
    }
948

    
949
    return 0;
950
}
951

    
952
static void v9fs_stat_free(V9fsStat *stat)
953
{
954
    v9fs_string_free(&stat->name);
955
    v9fs_string_free(&stat->uid);
956
    v9fs_string_free(&stat->gid);
957
    v9fs_string_free(&stat->muid);
958
    v9fs_string_free(&stat->extension);
959
}
960

    
961
static uint32_t stat_to_v9mode(const struct stat *stbuf)
962
{
963
    uint32_t mode;
964

    
965
    mode = stbuf->st_mode & 0777;
966
    if (S_ISDIR(stbuf->st_mode)) {
967
        mode |= P9_STAT_MODE_DIR;
968
    }
969

    
970
    if (S_ISLNK(stbuf->st_mode)) {
971
        mode |= P9_STAT_MODE_SYMLINK;
972
    }
973

    
974
    if (S_ISSOCK(stbuf->st_mode)) {
975
        mode |= P9_STAT_MODE_SOCKET;
976
    }
977

    
978
    if (S_ISFIFO(stbuf->st_mode)) {
979
        mode |= P9_STAT_MODE_NAMED_PIPE;
980
    }
981

    
982
    if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
983
        mode |= P9_STAT_MODE_DEVICE;
984
    }
985

    
986
    if (stbuf->st_mode & S_ISUID) {
987
        mode |= P9_STAT_MODE_SETUID;
988
    }
989

    
990
    if (stbuf->st_mode & S_ISGID) {
991
        mode |= P9_STAT_MODE_SETGID;
992
    }
993

    
994
    if (stbuf->st_mode & S_ISVTX) {
995
        mode |= P9_STAT_MODE_SETVTX;
996
    }
997

    
998
    return mode;
999
}
1000

    
1001
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1002
                            const struct stat *stbuf,
1003
                            V9fsStat *v9stat)
1004
{
1005
    int err;
1006
    const char *str;
1007

    
1008
    memset(v9stat, 0, sizeof(*v9stat));
1009

    
1010
    stat_to_qid(stbuf, &v9stat->qid);
1011
    v9stat->mode = stat_to_v9mode(stbuf);
1012
    v9stat->atime = stbuf->st_atime;
1013
    v9stat->mtime = stbuf->st_mtime;
1014
    v9stat->length = stbuf->st_size;
1015

    
1016
    v9fs_string_null(&v9stat->uid);
1017
    v9fs_string_null(&v9stat->gid);
1018
    v9fs_string_null(&v9stat->muid);
1019

    
1020
    v9stat->n_uid = stbuf->st_uid;
1021
    v9stat->n_gid = stbuf->st_gid;
1022
    v9stat->n_muid = 0;
1023

    
1024
    v9fs_string_null(&v9stat->extension);
1025

    
1026
    if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1027
        err = v9fs_do_readlink(s, name, &v9stat->extension);
1028
        if (err == -1) {
1029
            err = -errno;
1030
            return err;
1031
        }
1032
        v9stat->extension.data[err] = 0;
1033
        v9stat->extension.size = err;
1034
    } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1035
        v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1036
                S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1037
                major(stbuf->st_rdev), minor(stbuf->st_rdev));
1038
    } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1039
        v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1040
                "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1041
    }
1042

    
1043
    str = strrchr(name->data, '/');
1044
    if (str) {
1045
        str += 1;
1046
    } else {
1047
        str = name->data;
1048
    }
1049

    
1050
    v9fs_string_sprintf(&v9stat->name, "%s", str);
1051

    
1052
    v9stat->size = 61 +
1053
        v9fs_string_size(&v9stat->name) +
1054
        v9fs_string_size(&v9stat->uid) +
1055
        v9fs_string_size(&v9stat->gid) +
1056
        v9fs_string_size(&v9stat->muid) +
1057
        v9fs_string_size(&v9stat->extension);
1058
    return 0;
1059
}
1060

    
1061
#define P9_STATS_MODE          0x00000001ULL
1062
#define P9_STATS_NLINK         0x00000002ULL
1063
#define P9_STATS_UID           0x00000004ULL
1064
#define P9_STATS_GID           0x00000008ULL
1065
#define P9_STATS_RDEV          0x00000010ULL
1066
#define P9_STATS_ATIME         0x00000020ULL
1067
#define P9_STATS_MTIME         0x00000040ULL
1068
#define P9_STATS_CTIME         0x00000080ULL
1069
#define P9_STATS_INO           0x00000100ULL
1070
#define P9_STATS_SIZE          0x00000200ULL
1071
#define P9_STATS_BLOCKS        0x00000400ULL
1072

    
1073
#define P9_STATS_BTIME         0x00000800ULL
1074
#define P9_STATS_GEN           0x00001000ULL
1075
#define P9_STATS_DATA_VERSION  0x00002000ULL
1076

    
1077
#define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
1078
#define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
1079

    
1080

    
1081
static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1082
                            V9fsStatDotl *v9lstat)
1083
{
1084
    memset(v9lstat, 0, sizeof(*v9lstat));
1085

    
1086
    v9lstat->st_mode = stbuf->st_mode;
1087
    v9lstat->st_nlink = stbuf->st_nlink;
1088
    v9lstat->st_uid = stbuf->st_uid;
1089
    v9lstat->st_gid = stbuf->st_gid;
1090
    v9lstat->st_rdev = stbuf->st_rdev;
1091
    v9lstat->st_size = stbuf->st_size;
1092
    v9lstat->st_blksize = stbuf->st_blksize;
1093
    v9lstat->st_blocks = stbuf->st_blocks;
1094
    v9lstat->st_atime_sec = stbuf->st_atime;
1095
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1096
    v9lstat->st_mtime_sec = stbuf->st_mtime;
1097
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1098
    v9lstat->st_ctime_sec = stbuf->st_ctime;
1099
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1100
    /* Currently we only support BASIC fields in stat */
1101
    v9lstat->st_result_mask = P9_STATS_BASIC;
1102

    
1103
    stat_to_qid(stbuf, &v9lstat->qid);
1104
}
1105

    
1106
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1107
{
1108
    while (len && *iovcnt) {
1109
        if (len < sg->iov_len) {
1110
            sg->iov_len -= len;
1111
            sg->iov_base += len;
1112
            len = 0;
1113
        } else {
1114
            len -= sg->iov_len;
1115
            sg++;
1116
            *iovcnt -= 1;
1117
        }
1118
    }
1119

    
1120
    return sg;
1121
}
1122

    
1123
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1124
{
1125
    int i;
1126
    int total = 0;
1127

    
1128
    for (i = 0; i < *cnt; i++) {
1129
        if ((total + sg[i].iov_len) > cap) {
1130
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1131
            i++;
1132
            break;
1133
        }
1134
        total += sg[i].iov_len;
1135
    }
1136

    
1137
    *cnt = i;
1138

    
1139
    return sg;
1140
}
1141

    
1142
static void print_sg(struct iovec *sg, int cnt)
1143
{
1144
    int i;
1145

    
1146
    printf("sg[%d]: {", cnt);
1147
    for (i = 0; i < cnt; i++) {
1148
        if (i) {
1149
            printf(", ");
1150
        }
1151
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1152
    }
1153
    printf("}\n");
1154
}
1155

    
1156
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1157
{
1158
    V9fsString str;
1159
    v9fs_string_init(&str);
1160
    v9fs_string_copy(&str, dst);
1161
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1162
    v9fs_string_free(&str);
1163
}
1164

    
1165
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1166
{
1167
    V9fsString version;
1168
    size_t offset = 7;
1169

    
1170
    pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1171

    
1172
    if (!strcmp(version.data, "9P2000.u")) {
1173
        s->proto_version = V9FS_PROTO_2000U;
1174
    } else if (!strcmp(version.data, "9P2000.L")) {
1175
        s->proto_version = V9FS_PROTO_2000L;
1176
    } else {
1177
        v9fs_string_sprintf(&version, "unknown");
1178
    }
1179

    
1180
    offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1181
    complete_pdu(s, pdu, offset);
1182

    
1183
    v9fs_string_free(&version);
1184
}
1185

    
1186
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1187
{
1188
    int32_t fid, afid, n_uname;
1189
    V9fsString uname, aname;
1190
    V9fsFidState *fidp;
1191
    V9fsQID qid;
1192
    size_t offset = 7;
1193
    ssize_t err;
1194

    
1195
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1196

    
1197
    fidp = alloc_fid(s, fid);
1198
    if (fidp == NULL) {
1199
        err = -EINVAL;
1200
        goto out;
1201
    }
1202

    
1203
    fidp->uid = n_uname;
1204

    
1205
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1206
    err = fid_to_qid(s, fidp, &qid);
1207
    if (err) {
1208
        err = -EINVAL;
1209
        free_fid(s, fid);
1210
        goto out;
1211
    }
1212

    
1213
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1214

    
1215
    err = offset;
1216
out:
1217
    complete_pdu(s, pdu, err);
1218
    v9fs_string_free(&uname);
1219
    v9fs_string_free(&aname);
1220
}
1221

    
1222
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1223
{
1224
    if (err == -1) {
1225
        err = -errno;
1226
        goto out;
1227
    }
1228

    
1229
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1230
    if (err) {
1231
        goto out;
1232
    }
1233
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1234
    err = vs->offset;
1235

    
1236
out:
1237
    complete_pdu(s, vs->pdu, err);
1238
    v9fs_stat_free(&vs->v9stat);
1239
    qemu_free(vs);
1240
}
1241

    
1242
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1243
{
1244
    int32_t fid;
1245
    V9fsStatState *vs;
1246
    ssize_t err = 0;
1247

    
1248
    vs = qemu_malloc(sizeof(*vs));
1249
    vs->pdu = pdu;
1250
    vs->offset = 7;
1251

    
1252
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1253

    
1254
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1255

    
1256
    vs->fidp = lookup_fid(s, fid);
1257
    if (vs->fidp == NULL) {
1258
        err = -ENOENT;
1259
        goto out;
1260
    }
1261

    
1262
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1263
    v9fs_stat_post_lstat(s, vs, err);
1264
    return;
1265

    
1266
out:
1267
    complete_pdu(s, vs->pdu, err);
1268
    v9fs_stat_free(&vs->v9stat);
1269
    qemu_free(vs);
1270
}
1271

    
1272
static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1273
                                                                int err)
1274
{
1275
    if (err == -1) {
1276
        err = -errno;
1277
        goto out;
1278
    }
1279

    
1280
    stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1281
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1282
    err = vs->offset;
1283

    
1284
out:
1285
    complete_pdu(s, vs->pdu, err);
1286
    qemu_free(vs);
1287
}
1288

    
1289
static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1290
{
1291
    int32_t fid;
1292
    V9fsStatStateDotl *vs;
1293
    ssize_t err = 0;
1294
    V9fsFidState *fidp;
1295
    uint64_t request_mask;
1296

    
1297
    vs = qemu_malloc(sizeof(*vs));
1298
    vs->pdu = pdu;
1299
    vs->offset = 7;
1300

    
1301
    memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1302

    
1303
    pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1304

    
1305
    fidp = lookup_fid(s, fid);
1306
    if (fidp == NULL) {
1307
        err = -ENOENT;
1308
        goto out;
1309
    }
1310

    
1311
    /* Currently we only support BASIC fields in stat, so there is no
1312
     * need to look at request_mask.
1313
     */
1314
    err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1315
    v9fs_getattr_post_lstat(s, vs, err);
1316
    return;
1317

    
1318
out:
1319
    complete_pdu(s, vs->pdu, err);
1320
    qemu_free(vs);
1321
}
1322

    
1323
/* From Linux kernel code */
1324
#define ATTR_MODE    (1 << 0)
1325
#define ATTR_UID     (1 << 1)
1326
#define ATTR_GID     (1 << 2)
1327
#define ATTR_SIZE    (1 << 3)
1328
#define ATTR_ATIME   (1 << 4)
1329
#define ATTR_MTIME   (1 << 5)
1330
#define ATTR_CTIME   (1 << 6)
1331
#define ATTR_MASK    127
1332
#define ATTR_ATIME_SET  (1 << 7)
1333
#define ATTR_MTIME_SET  (1 << 8)
1334

    
1335
static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1336
                                                                  int err)
1337
{
1338
    if (err == -1) {
1339
        err = -errno;
1340
        goto out;
1341
    }
1342
    err = vs->offset;
1343

    
1344
out:
1345
    complete_pdu(s, vs->pdu, err);
1346
    qemu_free(vs);
1347
}
1348

    
1349
static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1350
{
1351
    if (err == -1) {
1352
        err = -errno;
1353
        goto out;
1354
    }
1355

    
1356
    if (vs->v9iattr.valid & (ATTR_SIZE)) {
1357
        err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1358
    }
1359
    v9fs_setattr_post_truncate(s, vs, err);
1360
    return;
1361

    
1362
out:
1363
    complete_pdu(s, vs->pdu, err);
1364
    qemu_free(vs);
1365
}
1366

    
1367
static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1368
                                                                   int err)
1369
{
1370
    if (err == -1) {
1371
        err = -errno;
1372
        goto out;
1373
    }
1374

    
1375
    /* If the only valid entry in iattr is ctime we can call
1376
     * chown(-1,-1) to update the ctime of the file
1377
     */
1378
    if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1379
            ((vs->v9iattr.valid & ATTR_CTIME)
1380
            && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1381
        if (!(vs->v9iattr.valid & ATTR_UID)) {
1382
            vs->v9iattr.uid = -1;
1383
        }
1384
        if (!(vs->v9iattr.valid & ATTR_GID)) {
1385
            vs->v9iattr.gid = -1;
1386
        }
1387
        err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1388
                                                vs->v9iattr.gid);
1389
    }
1390
    v9fs_setattr_post_chown(s, vs, err);
1391
    return;
1392

    
1393
out:
1394
    complete_pdu(s, vs->pdu, err);
1395
    qemu_free(vs);
1396
}
1397

    
1398
static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1399
{
1400
    if (err == -1) {
1401
        err = -errno;
1402
        goto out;
1403
    }
1404

    
1405
    if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1406
        struct timespec times[2];
1407
        if (vs->v9iattr.valid & ATTR_ATIME) {
1408
            if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1409
                times[0].tv_sec = vs->v9iattr.atime_sec;
1410
                times[0].tv_nsec = vs->v9iattr.atime_nsec;
1411
            } else {
1412
                times[0].tv_nsec = UTIME_NOW;
1413
            }
1414
        } else {
1415
            times[0].tv_nsec = UTIME_OMIT;
1416
        }
1417

    
1418
        if (vs->v9iattr.valid & ATTR_MTIME) {
1419
            if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1420
                times[1].tv_sec = vs->v9iattr.mtime_sec;
1421
                times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1422
            } else {
1423
                times[1].tv_nsec = UTIME_NOW;
1424
            }
1425
        } else {
1426
            times[1].tv_nsec = UTIME_OMIT;
1427
        }
1428
        err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1429
    }
1430
    v9fs_setattr_post_utimensat(s, vs, err);
1431
    return;
1432

    
1433
out:
1434
    complete_pdu(s, vs->pdu, err);
1435
    qemu_free(vs);
1436
}
1437

    
1438
static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1439
{
1440
    int32_t fid;
1441
    V9fsSetattrState *vs;
1442
    int err = 0;
1443

    
1444
    vs = qemu_malloc(sizeof(*vs));
1445
    vs->pdu = pdu;
1446
    vs->offset = 7;
1447

    
1448
    pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1449

    
1450
    vs->fidp = lookup_fid(s, fid);
1451
    if (vs->fidp == NULL) {
1452
        err = -EINVAL;
1453
        goto out;
1454
    }
1455

    
1456
    if (vs->v9iattr.valid & ATTR_MODE) {
1457
        err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1458
    }
1459

    
1460
    v9fs_setattr_post_chmod(s, vs, err);
1461
    return;
1462

    
1463
out:
1464
    complete_pdu(s, vs->pdu, err);
1465
    qemu_free(vs);
1466
}
1467

    
1468
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1469
{
1470
    complete_pdu(s, vs->pdu, err);
1471

    
1472
    if (vs->nwnames) {
1473
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1474
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1475
        }
1476

    
1477
        qemu_free(vs->wnames);
1478
        qemu_free(vs->qids);
1479
    }
1480
}
1481

    
1482
static void v9fs_walk_marshal(V9fsWalkState *vs)
1483
{
1484
    int i;
1485
    vs->offset = 7;
1486
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1487

    
1488
    for (i = 0; i < vs->nwnames; i++) {
1489
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1490
    }
1491
}
1492

    
1493
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1494
                                                                int err)
1495
{
1496
    if (err == -1) {
1497
        free_fid(s, vs->newfidp->fid);
1498
        v9fs_string_free(&vs->path);
1499
        err = -ENOENT;
1500
        goto out;
1501
    }
1502

    
1503
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1504

    
1505
    vs->name_idx++;
1506
    if (vs->name_idx < vs->nwnames) {
1507
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1508
                                            vs->wnames[vs->name_idx].data);
1509
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1510

    
1511
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1512
        v9fs_walk_post_newfid_lstat(s, vs, err);
1513
        return;
1514
    }
1515

    
1516
    v9fs_string_free(&vs->path);
1517
    v9fs_walk_marshal(vs);
1518
    err = vs->offset;
1519
out:
1520
    v9fs_walk_complete(s, vs, err);
1521
}
1522

    
1523
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1524
        int err)
1525
{
1526
    if (err == -1) {
1527
        v9fs_string_free(&vs->path);
1528
        err = -ENOENT;
1529
        goto out;
1530
    }
1531

    
1532
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1533
    vs->name_idx++;
1534
    if (vs->name_idx < vs->nwnames) {
1535

    
1536
        v9fs_string_sprintf(&vs->path, "%s/%s",
1537
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1538
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1539

    
1540
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1541
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1542
        return;
1543
    }
1544

    
1545
    v9fs_string_free(&vs->path);
1546
    v9fs_walk_marshal(vs);
1547
    err = vs->offset;
1548
out:
1549
    v9fs_walk_complete(s, vs, err);
1550
}
1551

    
1552
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1553
{
1554
    int32_t fid, newfid;
1555
    V9fsWalkState *vs;
1556
    int err = 0;
1557
    int i;
1558

    
1559
    vs = qemu_malloc(sizeof(*vs));
1560
    vs->pdu = pdu;
1561
    vs->wnames = NULL;
1562
    vs->qids = NULL;
1563
    vs->offset = 7;
1564

    
1565
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1566
                                            &newfid, &vs->nwnames);
1567

    
1568
    if (vs->nwnames) {
1569
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1570

    
1571
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1572

    
1573
        for (i = 0; i < vs->nwnames; i++) {
1574
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1575
                                            &vs->wnames[i]);
1576
        }
1577
    }
1578

    
1579
    vs->fidp = lookup_fid(s, fid);
1580
    if (vs->fidp == NULL) {
1581
        err = -ENOENT;
1582
        goto out;
1583
    }
1584

    
1585
    /* FIXME: is this really valid? */
1586
    if (fid == newfid) {
1587

    
1588
        BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1589
        v9fs_string_init(&vs->path);
1590
        vs->name_idx = 0;
1591

    
1592
        if (vs->name_idx < vs->nwnames) {
1593
            v9fs_string_sprintf(&vs->path, "%s/%s",
1594
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1595
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1596

    
1597
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1598
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1599
            return;
1600
        }
1601
    } else {
1602
        vs->newfidp = alloc_fid(s, newfid);
1603
        if (vs->newfidp == NULL) {
1604
            err = -EINVAL;
1605
            goto out;
1606
        }
1607

    
1608
        vs->newfidp->uid = vs->fidp->uid;
1609
        v9fs_string_init(&vs->path);
1610
        vs->name_idx = 0;
1611
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1612

    
1613
        if (vs->name_idx < vs->nwnames) {
1614
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1615
                                vs->wnames[vs->name_idx].data);
1616
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1617

    
1618
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1619
            v9fs_walk_post_newfid_lstat(s, vs, err);
1620
            return;
1621
        }
1622
    }
1623

    
1624
    v9fs_walk_marshal(vs);
1625
    err = vs->offset;
1626
out:
1627
    v9fs_walk_complete(s, vs, err);
1628
}
1629

    
1630
static int32_t get_iounit(V9fsState *s, V9fsString *name)
1631
{
1632
    struct statfs stbuf;
1633
    int32_t iounit = 0;
1634

    
1635
    /*
1636
     * iounit should be multiples of f_bsize (host filesystem block size
1637
     * and as well as less than (client msize - P9_IOHDRSZ))
1638
     */
1639
    if (!v9fs_do_statfs(s, name, &stbuf)) {
1640
        iounit = stbuf.f_bsize;
1641
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1642
    }
1643

    
1644
    if (!iounit) {
1645
        iounit = s->msize - P9_IOHDRSZ;
1646
    }
1647
    return iounit;
1648
}
1649

    
1650
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1651
{
1652
    if (vs->fidp->fs.dir == NULL) {
1653
        err = -errno;
1654
        goto out;
1655
    }
1656
    vs->fidp->fid_type = P9_FID_DIR;
1657
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1658
    err = vs->offset;
1659
out:
1660
    complete_pdu(s, vs->pdu, err);
1661
    qemu_free(vs);
1662

    
1663
}
1664

    
1665
static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1666
{
1667
    int err;
1668
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1669
    err = vs->offset;
1670
    complete_pdu(s, vs->pdu, err);
1671
    qemu_free(vs);
1672
}
1673

    
1674
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1675
{
1676
    if (vs->fidp->fs.fd == -1) {
1677
        err = -errno;
1678
        goto out;
1679
    }
1680
    vs->fidp->fid_type = P9_FID_FILE;
1681
    vs->iounit = get_iounit(s, &vs->fidp->path);
1682
    v9fs_open_post_getiounit(s, vs);
1683
    return;
1684
out:
1685
    complete_pdu(s, vs->pdu, err);
1686
    qemu_free(vs);
1687
}
1688

    
1689
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1690
{
1691
    int flags;
1692

    
1693
    if (err) {
1694
        err = -errno;
1695
        goto out;
1696
    }
1697

    
1698
    stat_to_qid(&vs->stbuf, &vs->qid);
1699

    
1700
    if (S_ISDIR(vs->stbuf.st_mode)) {
1701
        vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1702
        v9fs_open_post_opendir(s, vs, err);
1703
    } else {
1704
        if (s->proto_version == V9FS_PROTO_2000L) {
1705
            flags = vs->mode;
1706
            flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1707
        } else {
1708
            flags = omode_to_uflags(vs->mode);
1709
        }
1710
        vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1711
        v9fs_open_post_open(s, vs, err);
1712
    }
1713
    return;
1714
out:
1715
    complete_pdu(s, vs->pdu, err);
1716
    qemu_free(vs);
1717
}
1718

    
1719
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1720
{
1721
    int32_t fid;
1722
    V9fsOpenState *vs;
1723
    ssize_t err = 0;
1724

    
1725
    vs = qemu_malloc(sizeof(*vs));
1726
    vs->pdu = pdu;
1727
    vs->offset = 7;
1728
    vs->mode = 0;
1729

    
1730
    if (s->proto_version == V9FS_PROTO_2000L) {
1731
        pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1732
    } else {
1733
        pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1734
    }
1735

    
1736
    vs->fidp = lookup_fid(s, fid);
1737
    if (vs->fidp == NULL) {
1738
        err = -ENOENT;
1739
        goto out;
1740
    }
1741

    
1742
    BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1743

    
1744
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1745

    
1746
    v9fs_open_post_lstat(s, vs, err);
1747
    return;
1748
out:
1749
    complete_pdu(s, pdu, err);
1750
    qemu_free(vs);
1751
}
1752

    
1753
static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1754
{
1755
    if (err == 0) {
1756
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1757
        stat_to_qid(&vs->stbuf, &vs->qid);
1758
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1759
                &vs->iounit);
1760
        err = vs->offset;
1761
    } else {
1762
        vs->fidp->fid_type = P9_FID_NONE;
1763
        err = -errno;
1764
        if (vs->fidp->fs.fd > 0) {
1765
            close(vs->fidp->fs.fd);
1766
        }
1767
    }
1768

    
1769
    complete_pdu(s, vs->pdu, err);
1770
    v9fs_string_free(&vs->name);
1771
    v9fs_string_free(&vs->fullname);
1772
    qemu_free(vs);
1773
}
1774

    
1775
static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1776
        int err)
1777
{
1778
    if (err) {
1779
        err = -errno;
1780
        goto out;
1781
    }
1782
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1783

    
1784
out:
1785
    v9fs_post_lcreate(s, vs, err);
1786
}
1787

    
1788
static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1789
        int err)
1790
{
1791
    if (vs->fidp->fs.fd == -1) {
1792
        err = -errno;
1793
        goto out;
1794
    }
1795
    vs->fidp->fid_type = P9_FID_FILE;
1796
    vs->iounit =  get_iounit(s, &vs->fullname);
1797
    v9fs_lcreate_post_get_iounit(s, vs, err);
1798
    return;
1799

    
1800
out:
1801
    v9fs_post_lcreate(s, vs, err);
1802
}
1803

    
1804
static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1805
{
1806
    int32_t dfid, flags, mode;
1807
    gid_t gid;
1808
    V9fsLcreateState *vs;
1809
    ssize_t err = 0;
1810

    
1811
    vs = qemu_malloc(sizeof(*vs));
1812
    vs->pdu = pdu;
1813
    vs->offset = 7;
1814

    
1815
    v9fs_string_init(&vs->fullname);
1816

    
1817
    pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1818
            &mode, &gid);
1819

    
1820
    vs->fidp = lookup_fid(s, dfid);
1821
    if (vs->fidp == NULL) {
1822
        err = -ENOENT;
1823
        goto out;
1824
    }
1825

    
1826
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1827
             vs->name.data);
1828

    
1829
    vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1830
            gid, flags, mode);
1831
    v9fs_lcreate_post_do_open2(s, vs, err);
1832
    return;
1833

    
1834
out:
1835
    complete_pdu(s, vs->pdu, err);
1836
    v9fs_string_free(&vs->name);
1837
    qemu_free(vs);
1838
}
1839

    
1840
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1841
{
1842
    int32_t fid;
1843
    size_t offset = 7;
1844
    int err;
1845

    
1846
    pdu_unmarshal(pdu, offset, "d", &fid);
1847

    
1848
    err = free_fid(s, fid);
1849
    if (err < 0) {
1850
        goto out;
1851
    }
1852

    
1853
    offset = 7;
1854
    err = offset;
1855
out:
1856
    complete_pdu(s, pdu, err);
1857
}
1858

    
1859
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1860

    
1861
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1862
{
1863
    if (err) {
1864
        goto out;
1865
    }
1866
    v9fs_stat_free(&vs->v9stat);
1867
    v9fs_string_free(&vs->name);
1868
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1869
    vs->offset += vs->count;
1870
    err = vs->offset;
1871
out:
1872
    complete_pdu(s, vs->pdu, err);
1873
    qemu_free(vs);
1874
    return;
1875
}
1876

    
1877
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1878
                                    ssize_t err)
1879
{
1880
    if (err) {
1881
        err = -errno;
1882
        goto out;
1883
    }
1884
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1885
    if (err) {
1886
        goto out;
1887
    }
1888

    
1889
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1890
                            &vs->v9stat);
1891
    if ((vs->len != (vs->v9stat.size + 2)) ||
1892
            ((vs->count + vs->len) > vs->max_count)) {
1893
        v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1894
        v9fs_read_post_seekdir(s, vs, err);
1895
        return;
1896
    }
1897
    vs->count += vs->len;
1898
    v9fs_stat_free(&vs->v9stat);
1899
    v9fs_string_free(&vs->name);
1900
    vs->dir_pos = vs->dent->d_off;
1901
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1902
    v9fs_read_post_readdir(s, vs, err);
1903
    return;
1904
out:
1905
    v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1906
    v9fs_read_post_seekdir(s, vs, err);
1907
    return;
1908

    
1909
}
1910

    
1911
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1912
{
1913
    if (vs->dent) {
1914
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1915
        v9fs_string_init(&vs->name);
1916
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1917
                            vs->dent->d_name);
1918
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1919
        v9fs_read_post_dir_lstat(s, vs, err);
1920
        return;
1921
    }
1922

    
1923
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1924
    vs->offset += vs->count;
1925
    err = vs->offset;
1926
    complete_pdu(s, vs->pdu, err);
1927
    qemu_free(vs);
1928
    return;
1929
}
1930

    
1931
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1932
{
1933
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1934
    v9fs_read_post_readdir(s, vs, err);
1935
    return;
1936
}
1937

    
1938
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1939
                                       ssize_t err)
1940
{
1941
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1942
    v9fs_read_post_telldir(s, vs, err);
1943
    return;
1944
}
1945

    
1946
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1947
{
1948
    if (err  < 0) {
1949
        /* IO error return the error */
1950
        err = -errno;
1951
        goto out;
1952
    }
1953
    vs->total += vs->len;
1954
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1955
    if (vs->total < vs->count && vs->len > 0) {
1956
        do {
1957
            if (0) {
1958
                print_sg(vs->sg, vs->cnt);
1959
            }
1960
            vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1961
        } while (vs->len == -1 && errno == EINTR);
1962
        if (vs->len == -1) {
1963
            err  = -errno;
1964
        }
1965
        v9fs_read_post_readv(s, vs, err);
1966
        return;
1967
    }
1968
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1969
    vs->offset += vs->count;
1970
    err = vs->offset;
1971

    
1972
out:
1973
    complete_pdu(s, vs->pdu, err);
1974
    qemu_free(vs);
1975
}
1976

    
1977
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1978
{
1979
    if (err == -1) {
1980
        err = -errno;
1981
        goto out;
1982
    }
1983
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1984

    
1985
    if (vs->total < vs->count) {
1986
        do {
1987
            if (0) {
1988
                print_sg(vs->sg, vs->cnt);
1989
            }
1990
            vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1991
        } while (vs->len == -1 && errno == EINTR);
1992
        if (vs->len == -1) {
1993
            err  = -errno;
1994
        }
1995
        v9fs_read_post_readv(s, vs, err);
1996
        return;
1997
    }
1998
out:
1999
    complete_pdu(s, vs->pdu, err);
2000
    qemu_free(vs);
2001
}
2002

    
2003
static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2004
{
2005
    ssize_t err = 0;
2006
    int read_count;
2007
    int64_t xattr_len;
2008

    
2009
    xattr_len = vs->fidp->fs.xattr.len;
2010
    read_count = xattr_len - vs->off;
2011
    if (read_count > vs->count) {
2012
        read_count = vs->count;
2013
    } else if (read_count < 0) {
2014
        /*
2015
         * read beyond XATTR value
2016
         */
2017
        read_count = 0;
2018
    }
2019
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2020
    vs->offset += pdu_pack(vs->pdu, vs->offset,
2021
                           ((char *)vs->fidp->fs.xattr.value) + vs->off,
2022
                           read_count);
2023
    err = vs->offset;
2024
    complete_pdu(s, vs->pdu, err);
2025
    qemu_free(vs);
2026
}
2027

    
2028
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2029
{
2030
    int32_t fid;
2031
    V9fsReadState *vs;
2032
    ssize_t err = 0;
2033

    
2034
    vs = qemu_malloc(sizeof(*vs));
2035
    vs->pdu = pdu;
2036
    vs->offset = 7;
2037
    vs->total = 0;
2038
    vs->len = 0;
2039
    vs->count = 0;
2040

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

    
2043
    vs->fidp = lookup_fid(s, fid);
2044
    if (vs->fidp == NULL) {
2045
        err = -EINVAL;
2046
        goto out;
2047
    }
2048

    
2049
    if (vs->fidp->fid_type == P9_FID_DIR) {
2050
        vs->max_count = vs->count;
2051
        vs->count = 0;
2052
        if (vs->off == 0) {
2053
            v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2054
        }
2055
        v9fs_read_post_rewinddir(s, vs, err);
2056
        return;
2057
    } else if (vs->fidp->fid_type == P9_FID_FILE) {
2058
        vs->sg = vs->iov;
2059
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2060
        err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2061
        v9fs_read_post_lseek(s, vs, err);
2062
        return;
2063
    } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2064
        v9fs_xattr_read(s, vs);
2065
        return;
2066
    } else {
2067
        err = -EINVAL;
2068
    }
2069
out:
2070
    complete_pdu(s, pdu, err);
2071
    qemu_free(vs);
2072
}
2073

    
2074
typedef struct V9fsReadDirState {
2075
    V9fsPDU *pdu;
2076
    V9fsFidState *fidp;
2077
    V9fsQID qid;
2078
    off_t saved_dir_pos;
2079
    struct dirent *dent;
2080
    int32_t count;
2081
    int32_t max_count;
2082
    size_t offset;
2083
    int64_t initial_offset;
2084
    V9fsString name;
2085
} V9fsReadDirState;
2086

    
2087
static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2088
{
2089
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2090
    vs->offset += vs->count;
2091
    complete_pdu(s, vs->pdu, vs->offset);
2092
    qemu_free(vs);
2093
    return;
2094
}
2095

    
2096
/* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2097
 * size of type (1) + size of name.size (2) + strlen(name.data)
2098
 */
2099
#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2100

    
2101
static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2102
{
2103
    int len;
2104
    size_t size;
2105

    
2106
    if (vs->dent) {
2107
        v9fs_string_init(&vs->name);
2108
        v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2109

    
2110
        if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2111
            /* Ran out of buffer. Set dir back to old position and return */
2112
            v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2113
            v9fs_readdir_post_seekdir(s, vs);
2114
            return;
2115
        }
2116

    
2117
        /* Fill up just the path field of qid because the client uses
2118
         * only that. To fill the entire qid structure we will have
2119
         * to stat each dirent found, which is expensive
2120
         */
2121
        size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2122
        memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2123
        /* Fill the other fields with dummy values */
2124
        vs->qid.type = 0;
2125
        vs->qid.version = 0;
2126

    
2127
        len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2128
                              &vs->qid, vs->dent->d_off,
2129
                              vs->dent->d_type, &vs->name);
2130
        vs->count += len;
2131
        v9fs_string_free(&vs->name);
2132
        vs->saved_dir_pos = vs->dent->d_off;
2133
        vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2134
        v9fs_readdir_post_readdir(s, vs);
2135
        return;
2136
    }
2137

    
2138
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2139
    vs->offset += vs->count;
2140
    complete_pdu(s, vs->pdu, vs->offset);
2141
    qemu_free(vs);
2142
    return;
2143
}
2144

    
2145
static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2146
{
2147
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2148
    v9fs_readdir_post_readdir(s, vs);
2149
    return;
2150
}
2151

    
2152
static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2153
{
2154
    vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2155
    v9fs_readdir_post_telldir(s, vs);
2156
    return;
2157
}
2158

    
2159
static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2160
{
2161
    int32_t fid;
2162
    V9fsReadDirState *vs;
2163
    ssize_t err = 0;
2164
    size_t offset = 7;
2165

    
2166
    vs = qemu_malloc(sizeof(*vs));
2167
    vs->pdu = pdu;
2168
    vs->offset = 7;
2169
    vs->count = 0;
2170

    
2171
    pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2172
                                                        &vs->max_count);
2173

    
2174
    vs->fidp = lookup_fid(s, fid);
2175
    if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2176
        err = -EINVAL;
2177
        goto out;
2178
    }
2179

    
2180
    if (vs->initial_offset == 0) {
2181
        v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2182
    } else {
2183
        v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2184
    }
2185

    
2186
    v9fs_readdir_post_setdir(s, vs);
2187
    return;
2188

    
2189
out:
2190
    complete_pdu(s, pdu, err);
2191
    qemu_free(vs);
2192
    return;
2193
}
2194

    
2195
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
2196
                                   ssize_t err)
2197
{
2198
    if (err  < 0) {
2199
        /* IO error return the error */
2200
        err = -errno;
2201
        goto out;
2202
    }
2203
    vs->total += vs->len;
2204
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2205
    if (vs->total < vs->count && vs->len > 0) {
2206
        do {
2207
            if (0) {
2208
                print_sg(vs->sg, vs->cnt);
2209
            }
2210
            vs->len =  v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2211
        } while (vs->len == -1 && errno == EINTR);
2212
        if (vs->len == -1) {
2213
            err  = -errno;
2214
        }
2215
        v9fs_write_post_writev(s, vs, err);
2216
        return;
2217
    }
2218
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2219

    
2220
    err = vs->offset;
2221
out:
2222
    complete_pdu(s, vs->pdu, err);
2223
    qemu_free(vs);
2224
}
2225

    
2226
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
2227
{
2228
    if (err == -1) {
2229
        err = -errno;
2230
        goto out;
2231
    }
2232
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2233

    
2234
    if (vs->total < vs->count) {
2235
        do {
2236
            if (0) {
2237
                print_sg(vs->sg, vs->cnt);
2238
            }
2239
            vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2240
        } while (vs->len == -1 && errno == EINTR);
2241
        if (vs->len == -1) {
2242
            err  = -errno;
2243
        }
2244
        v9fs_write_post_writev(s, vs, err);
2245
        return;
2246
    }
2247

    
2248
out:
2249
    complete_pdu(s, vs->pdu, err);
2250
    qemu_free(vs);
2251
}
2252

    
2253
static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2254
{
2255
    int i, to_copy;
2256
    ssize_t err = 0;
2257
    int write_count;
2258
    int64_t xattr_len;
2259

    
2260
    xattr_len = vs->fidp->fs.xattr.len;
2261
    write_count = xattr_len - vs->off;
2262
    if (write_count > vs->count) {
2263
        write_count = vs->count;
2264
    } else if (write_count < 0) {
2265
        /*
2266
         * write beyond XATTR value len specified in
2267
         * xattrcreate
2268
         */
2269
        err = -ENOSPC;
2270
        goto out;
2271
    }
2272
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2273
    err = vs->offset;
2274
    vs->fidp->fs.xattr.copied_len += write_count;
2275
    /*
2276
     * Now copy the content from sg list
2277
     */
2278
    for (i = 0; i < vs->cnt; i++) {
2279
        if (write_count > vs->sg[i].iov_len) {
2280
            to_copy = vs->sg[i].iov_len;
2281
        } else {
2282
            to_copy = write_count;
2283
        }
2284
        memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2285
               vs->sg[i].iov_base, to_copy);
2286
        /* updating vs->off since we are not using below */
2287
        vs->off += to_copy;
2288
        write_count -= to_copy;
2289
    }
2290
out:
2291
    complete_pdu(s, vs->pdu, err);
2292
    qemu_free(vs);
2293
}
2294

    
2295
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2296
{
2297
    int32_t fid;
2298
    V9fsWriteState *vs;
2299
    ssize_t err;
2300

    
2301
    vs = qemu_malloc(sizeof(*vs));
2302

    
2303
    vs->pdu = pdu;
2304
    vs->offset = 7;
2305
    vs->sg = vs->iov;
2306
    vs->total = 0;
2307
    vs->len = 0;
2308

    
2309
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2310
                  vs->sg, &vs->cnt);
2311

    
2312
    vs->fidp = lookup_fid(s, fid);
2313
    if (vs->fidp == NULL) {
2314
        err = -EINVAL;
2315
        goto out;
2316
    }
2317

    
2318
    if (vs->fidp->fid_type == P9_FID_FILE) {
2319
        if (vs->fidp->fs.fd == -1) {
2320
            err = -EINVAL;
2321
            goto out;
2322
        }
2323
    } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2324
        /*
2325
         * setxattr operation
2326
         */
2327
        v9fs_xattr_write(s, vs);
2328
        return;
2329
    } else {
2330
        err = -EINVAL;
2331
        goto out;
2332
    }
2333
    err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2334

    
2335
    v9fs_write_post_lseek(s, vs, err);
2336
    return;
2337

    
2338
out:
2339
    complete_pdu(s, vs->pdu, err);
2340
    qemu_free(vs);
2341
}
2342

    
2343
static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2344
{
2345
    int err;
2346
    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2347
    stat_to_qid(&vs->stbuf, &vs->qid);
2348

    
2349
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2350
    err = vs->offset;
2351

    
2352
    complete_pdu(s, vs->pdu, err);
2353
    v9fs_string_free(&vs->name);
2354
    v9fs_string_free(&vs->extension);
2355
    v9fs_string_free(&vs->fullname);
2356
    qemu_free(vs);
2357
}
2358

    
2359
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2360
{
2361
    if (err == 0) {
2362
        vs->iounit = get_iounit(s, &vs->fidp->path);
2363
        v9fs_create_post_getiounit(s, vs);
2364
        return;
2365
    }
2366

    
2367
    complete_pdu(s, vs->pdu, err);
2368
    v9fs_string_free(&vs->name);
2369
    v9fs_string_free(&vs->extension);
2370
    v9fs_string_free(&vs->fullname);
2371
    qemu_free(vs);
2372
}
2373

    
2374
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2375
{
2376
    if (err) {
2377
        err = -errno;
2378
    }
2379
    v9fs_post_create(s, vs, err);
2380
}
2381

    
2382
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2383
                                                                    int err)
2384
{
2385
    if (!vs->fidp->fs.dir) {
2386
        err = -errno;
2387
    }
2388
    vs->fidp->fid_type = P9_FID_DIR;
2389
    v9fs_post_create(s, vs, err);
2390
}
2391

    
2392
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2393
                                                                    int err)
2394
{
2395
    if (err) {
2396
        err = -errno;
2397
        goto out;
2398
    }
2399

    
2400
    vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2401
    v9fs_create_post_opendir(s, vs, err);
2402
    return;
2403

    
2404
out:
2405
    v9fs_post_create(s, vs, err);
2406
}
2407

    
2408
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2409
{
2410
    if (err) {
2411
        err = -errno;
2412
        goto out;
2413
    }
2414

    
2415
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2416
    v9fs_create_post_dir_lstat(s, vs, err);
2417
    return;
2418

    
2419
out:
2420
    v9fs_post_create(s, vs, err);
2421
}
2422

    
2423
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2424
{
2425
    if (err) {
2426
        vs->fidp->fid_type = P9_FID_NONE;
2427
        close(vs->fidp->fs.fd);
2428
        err = -errno;
2429
    }
2430
    v9fs_post_create(s, vs, err);
2431
    return;
2432
}
2433

    
2434
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2435
{
2436
    if (vs->fidp->fs.fd == -1) {
2437
        err = -errno;
2438
        goto out;
2439
    }
2440
    vs->fidp->fid_type = P9_FID_FILE;
2441
    err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2442
    v9fs_create_post_fstat(s, vs, err);
2443

    
2444
    return;
2445

    
2446
out:
2447
    v9fs_post_create(s, vs, err);
2448

    
2449
}
2450

    
2451
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2452
{
2453

    
2454
    if (err == 0 || errno != ENOENT) {
2455
        err = -errno;
2456
        goto out;
2457
    }
2458

    
2459
    if (vs->perm & P9_STAT_MODE_DIR) {
2460
        err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2461
                vs->fidp->uid, -1);
2462
        v9fs_create_post_mkdir(s, vs, err);
2463
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2464
        err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2465
                vs->fullname.data, -1);
2466
        v9fs_create_post_perms(s, vs, err);
2467
    } else if (vs->perm & P9_STAT_MODE_LINK) {
2468
        int32_t nfid = atoi(vs->extension.data);
2469
        V9fsFidState *nfidp = lookup_fid(s, nfid);
2470
        if (nfidp == NULL) {
2471
            err = -errno;
2472
            v9fs_post_create(s, vs, err);
2473
        }
2474
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2475
        v9fs_create_post_perms(s, vs, err);
2476
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2477
        char ctype;
2478
        uint32_t major, minor;
2479
        mode_t nmode = 0;
2480

    
2481
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2482
                                        &minor) != 3) {
2483
            err = -errno;
2484
            v9fs_post_create(s, vs, err);
2485
        }
2486

    
2487
        switch (ctype) {
2488
        case 'c':
2489
            nmode = S_IFCHR;
2490
            break;
2491
        case 'b':
2492
            nmode = S_IFBLK;
2493
            break;
2494
        default:
2495
            err = -EIO;
2496
            v9fs_post_create(s, vs, err);
2497
        }
2498

    
2499
        nmode |= vs->perm & 0777;
2500
        err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2501
                makedev(major, minor), vs->fidp->uid, -1);
2502
        v9fs_create_post_perms(s, vs, err);
2503
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2504
        err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2505
                0, vs->fidp->uid, -1);
2506
        v9fs_post_create(s, vs, err);
2507
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2508
        err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2509
                0, vs->fidp->uid, -1);
2510
        v9fs_post_create(s, vs, err);
2511
    } else {
2512
        vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2513
                -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2514

    
2515
        v9fs_create_post_open2(s, vs, err);
2516
    }
2517

    
2518
    return;
2519

    
2520
out:
2521
    v9fs_post_create(s, vs, err);
2522
}
2523

    
2524
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2525
{
2526
    int32_t fid;
2527
    V9fsCreateState *vs;
2528
    int err = 0;
2529

    
2530
    vs = qemu_malloc(sizeof(*vs));
2531
    vs->pdu = pdu;
2532
    vs->offset = 7;
2533

    
2534
    v9fs_string_init(&vs->fullname);
2535

    
2536
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2537
                                &vs->perm, &vs->mode, &vs->extension);
2538

    
2539
    vs->fidp = lookup_fid(s, fid);
2540
    if (vs->fidp == NULL) {
2541
        err = -EINVAL;
2542
        goto out;
2543
    }
2544

    
2545
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2546
                                                        vs->name.data);
2547

    
2548
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2549
    v9fs_create_post_lstat(s, vs, err);
2550
    return;
2551

    
2552
out:
2553
    complete_pdu(s, vs->pdu, err);
2554
    v9fs_string_free(&vs->name);
2555
    v9fs_string_free(&vs->extension);
2556
    qemu_free(vs);
2557
}
2558

    
2559
static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2560
{
2561
    if (err == 0) {
2562
        stat_to_qid(&vs->stbuf, &vs->qid);
2563
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2564
        err = vs->offset;
2565
    } else {
2566
        err = -errno;
2567
    }
2568
    complete_pdu(s, vs->pdu, err);
2569
    v9fs_string_free(&vs->name);
2570
    v9fs_string_free(&vs->symname);
2571
    v9fs_string_free(&vs->fullname);
2572
    qemu_free(vs);
2573
}
2574

    
2575
static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2576
        int err)
2577
{
2578
    if (err) {
2579
        goto out;
2580
    }
2581
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2582
out:
2583
    v9fs_post_symlink(s, vs, err);
2584
}
2585

    
2586
static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2587
{
2588
    int32_t dfid;
2589
    V9fsSymlinkState *vs;
2590
    int err = 0;
2591
    gid_t gid;
2592

    
2593
    vs = qemu_malloc(sizeof(*vs));
2594
    vs->pdu = pdu;
2595
    vs->offset = 7;
2596

    
2597
    v9fs_string_init(&vs->fullname);
2598

    
2599
    pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2600
            &vs->symname, &gid);
2601

    
2602
    vs->dfidp = lookup_fid(s, dfid);
2603
    if (vs->dfidp == NULL) {
2604
        err = -EINVAL;
2605
        goto out;
2606
    }
2607

    
2608
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2609
            vs->name.data);
2610
    err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2611
            vs->fullname.data, gid);
2612
    v9fs_symlink_post_do_symlink(s, vs, err);
2613
    return;
2614

    
2615
out:
2616
    complete_pdu(s, vs->pdu, err);
2617
    v9fs_string_free(&vs->name);
2618
    v9fs_string_free(&vs->symname);
2619
    qemu_free(vs);
2620
}
2621

    
2622
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2623
{
2624
    /* A nop call with no return */
2625
    complete_pdu(s, pdu, 7);
2626
}
2627

    
2628
static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2629
{
2630
    int32_t dfid, oldfid;
2631
    V9fsFidState *dfidp, *oldfidp;
2632
    V9fsString name, fullname;
2633
    size_t offset = 7;
2634
    int err = 0;
2635

    
2636
    v9fs_string_init(&fullname);
2637

    
2638
    pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2639

    
2640
    dfidp = lookup_fid(s, dfid);
2641
    if (dfidp == NULL) {
2642
        err = -errno;
2643
        goto out;
2644
    }
2645

    
2646
    oldfidp = lookup_fid(s, oldfid);
2647
    if (oldfidp == NULL) {
2648
        err = -errno;
2649
        goto out;
2650
    }
2651

    
2652
    v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2653
    err = offset;
2654
    err = v9fs_do_link(s, &oldfidp->path, &fullname);
2655
    if (err) {
2656
        err = -errno;
2657
    }
2658
    v9fs_string_free(&fullname);
2659

    
2660
out:
2661
    v9fs_string_free(&name);
2662
    complete_pdu(s, pdu, err);
2663
}
2664

    
2665
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2666
                                                                int err)
2667
{
2668
    if (err < 0) {
2669
        err = -errno;
2670
    } else {
2671
        err = vs->offset;
2672
    }
2673

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

    
2677
    complete_pdu(s, vs->pdu, err);
2678
    qemu_free(vs);
2679
}
2680

    
2681
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2682
{
2683
    int32_t fid;
2684
    V9fsRemoveState *vs;
2685
    int err = 0;
2686

    
2687
    vs = qemu_malloc(sizeof(*vs));
2688
    vs->pdu = pdu;
2689
    vs->offset = 7;
2690

    
2691
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2692

    
2693
    vs->fidp = lookup_fid(s, fid);
2694
    if (vs->fidp == NULL) {
2695
        err = -EINVAL;
2696
        goto out;
2697
    }
2698

    
2699
    err = v9fs_do_remove(s, &vs->fidp->path);
2700
    v9fs_remove_post_remove(s, vs, err);
2701
    return;
2702

    
2703
out:
2704
    complete_pdu(s, pdu, err);
2705
    qemu_free(vs);
2706
}
2707

    
2708
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2709
{
2710
    if (err < 0) {
2711
        goto out;
2712
    }
2713

    
2714
    err = vs->offset;
2715

    
2716
out:
2717
    v9fs_stat_free(&vs->v9stat);
2718
    complete_pdu(s, vs->pdu, err);
2719
    qemu_free(vs);
2720
}
2721

    
2722
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2723
{
2724
    if (err < 0) {
2725
        goto out;
2726
    }
2727
    if (vs->v9stat.length != -1) {
2728
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2729
            err = -errno;
2730
        }
2731
    }
2732
    v9fs_wstat_post_truncate(s, vs, err);
2733
    return;
2734

    
2735
out:
2736
    v9fs_stat_free(&vs->v9stat);
2737
    complete_pdu(s, vs->pdu, err);
2738
    qemu_free(vs);
2739
}
2740

    
2741
static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2742
{
2743
    int err = 0;
2744
    char *old_name, *new_name;
2745
    char *end;
2746

    
2747
    if (vs->newdirfid != -1) {
2748
        V9fsFidState *dirfidp;
2749
        dirfidp = lookup_fid(s, vs->newdirfid);
2750

    
2751
        if (dirfidp == NULL) {
2752
            err = -ENOENT;
2753
            goto out;
2754
        }
2755

    
2756
        BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2757

    
2758
        new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2759

    
2760
        strcpy(new_name, dirfidp->path.data);
2761
        strcat(new_name, "/");
2762
        strcat(new_name + dirfidp->path.size, vs->name.data);
2763
    } else {
2764
        old_name = vs->fidp->path.data;
2765
        end = strrchr(old_name, '/');
2766
        if (end) {
2767
            end++;
2768
        } else {
2769
            end = old_name;
2770
        }
2771
        new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2772

    
2773
        strncat(new_name, old_name, end - old_name);
2774
        strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2775
    }
2776

    
2777
    v9fs_string_free(&vs->name);
2778
    vs->name.data = qemu_strdup(new_name);
2779
    vs->name.size = strlen(new_name);
2780

    
2781
    if (strcmp(new_name, vs->fidp->path.data) != 0) {
2782
        if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2783
            err = -errno;
2784
        } else {
2785
            V9fsFidState *fidp;
2786
            /*
2787
            * Fixup fid's pointing to the old name to
2788
            * start pointing to the new name
2789
            */
2790
            for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2791
                if (vs->fidp == fidp) {
2792
                    /*
2793
                    * we replace name of this fid towards the end
2794
                    * so that our below strcmp will work
2795
                    */
2796
                    continue;
2797
                }
2798
                if (!strncmp(vs->fidp->path.data, fidp->path.data,
2799
                    strlen(vs->fidp->path.data))) {
2800
                    /* replace the name */
2801
                    v9fs_fix_path(&fidp->path, &vs->name,
2802
                                  strlen(vs->fidp->path.data));
2803
                }
2804
            }
2805
            v9fs_string_copy(&vs->fidp->path, &vs->name);
2806
        }
2807
    }
2808
out:
2809
    v9fs_string_free(&vs->name);
2810
    return err;
2811
}
2812

    
2813
static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2814
{
2815
    complete_pdu(s, vs->pdu, err);
2816
    qemu_free(vs);
2817
}
2818

    
2819
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2820
{
2821
    if (err < 0) {
2822
        goto out;
2823
    }
2824

    
2825
    if (vs->v9stat.name.size != 0) {
2826
        V9fsRenameState *vr;
2827

    
2828
        vr = qemu_mallocz(sizeof(V9fsRenameState));
2829
        vr->newdirfid = -1;
2830
        vr->pdu = vs->pdu;
2831
        vr->fidp = vs->fidp;
2832
        vr->offset = vs->offset;
2833
        vr->name.size = vs->v9stat.name.size;
2834
        vr->name.data = qemu_strdup(vs->v9stat.name.data);
2835

    
2836
        err = v9fs_complete_rename(s, vr);
2837
        qemu_free(vr);
2838
    }
2839
    v9fs_wstat_post_rename(s, vs, err);
2840
    return;
2841

    
2842
out:
2843
    v9fs_stat_free(&vs->v9stat);
2844
    complete_pdu(s, vs->pdu, err);
2845
    qemu_free(vs);
2846
}
2847

    
2848
static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2849
{
2850
    int32_t fid;
2851
    V9fsRenameState *vs;
2852
    ssize_t err = 0;
2853

    
2854
    vs = qemu_malloc(sizeof(*vs));
2855
    vs->pdu = pdu;
2856
    vs->offset = 7;
2857

    
2858
    pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2859

    
2860
    vs->fidp = lookup_fid(s, fid);
2861
    if (vs->fidp == NULL) {
2862
        err = -ENOENT;
2863
        goto out;
2864
    }
2865

    
2866
    BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2867

    
2868
    err = v9fs_complete_rename(s, vs);
2869
    v9fs_rename_post_rename(s, vs, err);
2870
    return;
2871
out:
2872
    complete_pdu(s, vs->pdu, err);
2873
    qemu_free(vs);
2874
}
2875

    
2876
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2877
{
2878
    if (err < 0) {
2879
        goto out;
2880
    }
2881

    
2882
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2883
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2884
                    vs->v9stat.n_gid)) {
2885
            err = -errno;
2886
        }
2887
    }
2888
    v9fs_wstat_post_chown(s, vs, err);
2889
    return;
2890

    
2891
out:
2892
    v9fs_stat_free(&vs->v9stat);
2893
    complete_pdu(s, vs->pdu, err);
2894
    qemu_free(vs);
2895
}
2896

    
2897
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2898
{
2899
    if (err < 0) {
2900
        goto out;
2901
    }
2902

    
2903
    if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2904
        struct timespec times[2];
2905
        if (vs->v9stat.atime != -1) {
2906
            times[0].tv_sec = vs->v9stat.atime;
2907
            times[0].tv_nsec = 0;
2908
        } else {
2909
            times[0].tv_nsec = UTIME_OMIT;
2910
        }
2911
        if (vs->v9stat.mtime != -1) {
2912
            times[1].tv_sec = vs->v9stat.mtime;
2913
            times[1].tv_nsec = 0;
2914
        } else {
2915
            times[1].tv_nsec = UTIME_OMIT;
2916
        }
2917

    
2918
        if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2919
            err = -errno;
2920
        }
2921
    }
2922

    
2923
    v9fs_wstat_post_utime(s, vs, err);
2924
    return;
2925

    
2926
out:
2927
    v9fs_stat_free(&vs->v9stat);
2928
    complete_pdu(s, vs->pdu, err);
2929
    qemu_free(vs);
2930
}
2931

    
2932
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2933
{
2934
    if (err == -1) {
2935
        err = -errno;
2936
    }
2937
    v9fs_stat_free(&vs->v9stat);
2938
    complete_pdu(s, vs->pdu, err);
2939
    qemu_free(vs);
2940
}
2941

    
2942
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2943
{
2944
    uint32_t v9_mode;
2945

    
2946
    if (err == -1) {
2947
        err = -errno;
2948
        goto out;
2949
    }
2950

    
2951
    v9_mode = stat_to_v9mode(&vs->stbuf);
2952

    
2953
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2954
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2955
            /* Attempting to change the type */
2956
            err = -EIO;
2957
            goto out;
2958
    }
2959

    
2960
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2961
                    &vs->v9stat.extension))) {
2962
            err = -errno;
2963
     }
2964
    v9fs_wstat_post_chmod(s, vs, err);
2965
    return;
2966

    
2967
out:
2968
    v9fs_stat_free(&vs->v9stat);
2969
    complete_pdu(s, vs->pdu, err);
2970
    qemu_free(vs);
2971
}
2972

    
2973
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2974
{
2975
    int32_t fid;
2976
    V9fsWstatState *vs;
2977
    int err = 0;
2978

    
2979
    vs = qemu_malloc(sizeof(*vs));
2980
    vs->pdu = pdu;
2981
    vs->offset = 7;
2982

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

    
2985
    vs->fidp = lookup_fid(s, fid);
2986
    if (vs->fidp == NULL) {
2987
        err = -EINVAL;
2988
        goto out;
2989
    }
2990

    
2991
    /* do we need to sync the file? */
2992
    if (donttouch_stat(&vs->v9stat)) {
2993
        err = v9fs_do_fsync(s, vs->fidp->fs.fd);
2994
        v9fs_wstat_post_fsync(s, vs, err);
2995
        return;
2996
    }
2997

    
2998
    if (vs->v9stat.mode != -1) {
2999
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3000
        v9fs_wstat_post_lstat(s, vs, err);
3001
        return;
3002
    }
3003

    
3004
    v9fs_wstat_post_chmod(s, vs, err);
3005
    return;
3006

    
3007
out:
3008
    v9fs_stat_free(&vs->v9stat);
3009
    complete_pdu(s, vs->pdu, err);
3010
    qemu_free(vs);
3011
}
3012

    
3013
static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3014
{
3015
    int32_t bsize_factor;
3016

    
3017
    if (err) {
3018
        err = -errno;
3019
        goto out;
3020
    }
3021

    
3022
    /*
3023
     * compute bsize factor based on host file system block size
3024
     * and client msize
3025
     */
3026
    bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3027
    if (!bsize_factor) {
3028
        bsize_factor = 1;
3029
    }
3030
    vs->v9statfs.f_type = vs->stbuf.f_type;
3031
    vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3032
    vs->v9statfs.f_bsize *= bsize_factor;
3033
    /*
3034
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3035
     * adjust(divide) the number of blocks, free blocks and available
3036
     * blocks by bsize factor
3037
     */
3038
    vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3039
    vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3040
    vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3041
    vs->v9statfs.f_files = vs->stbuf.f_files;
3042
    vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3043
    vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3044
                        (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3045
    vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3046

    
3047
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3048
         vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3049
         vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3050
         vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3051
         vs->v9statfs.f_namelen);
3052

    
3053
out:
3054
    complete_pdu(s, vs->pdu, vs->offset);
3055
    qemu_free(vs);
3056
}
3057

    
3058
static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3059
{
3060
    V9fsStatfsState *vs;
3061
    ssize_t err = 0;
3062

    
3063
    vs = qemu_malloc(sizeof(*vs));
3064
    vs->pdu = pdu;
3065
    vs->offset = 7;
3066

    
3067
    memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3068

    
3069
    pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3070

    
3071
    vs->fidp = lookup_fid(s, vs->fid);
3072
    if (vs->fidp == NULL) {
3073
        err = -ENOENT;
3074
        goto out;
3075
    }
3076

    
3077
    err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3078
    v9fs_statfs_post_statfs(s, vs, err);
3079
    return;
3080

    
3081
out:
3082
    complete_pdu(s, vs->pdu, err);
3083
    qemu_free(vs);
3084
}
3085

    
3086
static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3087
{
3088
    if (err == -1) {
3089
        err = -errno;
3090
        goto out;
3091
    }
3092

    
3093
    stat_to_qid(&vs->stbuf, &vs->qid);
3094
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3095
    err = vs->offset;
3096
out:
3097
    complete_pdu(s, vs->pdu, err);
3098
    v9fs_string_free(&vs->fullname);
3099
    v9fs_string_free(&vs->name);
3100
    qemu_free(vs);
3101
}
3102

    
3103
static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3104
{
3105
    if (err == -1) {
3106
        err = -errno;
3107
        goto out;
3108
    }
3109

    
3110
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3111
    v9fs_mknod_post_lstat(s, vs, err);
3112
    return;
3113
out:
3114
    complete_pdu(s, vs->pdu, err);
3115
    v9fs_string_free(&vs->fullname);
3116
    v9fs_string_free(&vs->name);
3117
    qemu_free(vs);
3118
}
3119

    
3120
static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3121
{
3122
    int32_t fid;
3123
    V9fsMkState *vs;
3124
    int err = 0;
3125
    V9fsFidState *fidp;
3126
    gid_t gid;
3127
    int mode;
3128
    int major, minor;
3129

    
3130
    vs = qemu_malloc(sizeof(*vs));
3131
    vs->pdu = pdu;
3132
    vs->offset = 7;
3133

    
3134
    v9fs_string_init(&vs->fullname);
3135
    pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3136
        &major, &minor, &gid);
3137

    
3138
    fidp = lookup_fid(s, fid);
3139
    if (fidp == NULL) {
3140
        err = -ENOENT;
3141
        goto out;
3142
    }
3143

    
3144
    v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3145
    err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3146
        fidp->uid, gid);
3147
    v9fs_mknod_post_mknod(s, vs, err);
3148
    return;
3149

    
3150
out:
3151
    complete_pdu(s, vs->pdu, err);
3152
    v9fs_string_free(&vs->fullname);
3153
    v9fs_string_free(&vs->name);
3154
    qemu_free(vs);
3155
}
3156

    
3157
static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3158
{
3159
    if (err == -1) {
3160
        err = -errno;
3161
        goto out;
3162
    }
3163

    
3164
    stat_to_qid(&vs->stbuf, &vs->qid);
3165
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3166
    err = vs->offset;
3167
out:
3168
    complete_pdu(s, vs->pdu, err);
3169
    v9fs_string_free(&vs->fullname);
3170
    v9fs_string_free(&vs->name);
3171
    qemu_free(vs);
3172
}
3173

    
3174
static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3175
{
3176
    if (err == -1) {
3177
        err = -errno;
3178
        goto out;
3179
    }
3180

    
3181
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3182
    v9fs_mkdir_post_lstat(s, vs, err);
3183
    return;
3184
out:
3185
    complete_pdu(s, vs->pdu, err);
3186
    v9fs_string_free(&vs->fullname);
3187
    v9fs_string_free(&vs->name);
3188
    qemu_free(vs);
3189
}
3190

    
3191
static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3192
{
3193
    int32_t fid;
3194
    V9fsMkState *vs;
3195
    int err = 0;
3196
    V9fsFidState *fidp;
3197
    gid_t gid;
3198
    int mode;
3199

    
3200
    vs = qemu_malloc(sizeof(*vs));
3201
    vs->pdu = pdu;
3202
    vs->offset = 7;
3203

    
3204
    v9fs_string_init(&vs->fullname);
3205
    pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3206
        &gid);
3207

    
3208
    fidp = lookup_fid(s, fid);
3209
    if (fidp == NULL) {
3210
        err = -ENOENT;
3211
        goto out;
3212
    }
3213

    
3214
    v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3215
    err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3216
    v9fs_mkdir_post_mkdir(s, vs, err);
3217
    return;
3218

    
3219
out:
3220
    complete_pdu(s, vs->pdu, err);
3221
    v9fs_string_free(&vs->fullname);
3222
    v9fs_string_free(&vs->name);
3223
    qemu_free(vs);
3224
}
3225

    
3226
static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3227
{
3228

    
3229
    if (err < 0) {
3230
        err = -errno;
3231
        free_fid(s, vs->xattr_fidp->fid);
3232
        goto out;
3233
    }
3234
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3235
    err = vs->offset;
3236
out:
3237
    complete_pdu(s, vs->pdu, err);
3238
    v9fs_string_free(&vs->name);
3239
    qemu_free(vs);
3240
    return;
3241
}
3242

    
3243
static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3244
{
3245
    if (err < 0) {
3246
        err = -errno;
3247
        free_fid(s, vs->xattr_fidp->fid);
3248
        goto out;
3249
    }
3250
    /*
3251
     * Read the xattr value
3252
     */
3253
    vs->xattr_fidp->fs.xattr.len = vs->size;
3254
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3255
    vs->xattr_fidp->fs.xattr.copied_len = -1;
3256
    if (vs->size) {
3257
        vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3258
        err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3259
                                &vs->name, vs->xattr_fidp->fs.xattr.value,
3260
                                vs->xattr_fidp->fs.xattr.len);
3261
    }
3262
    v9fs_post_xattr_getvalue(s, vs, err);
3263
    return;
3264
out:
3265
    complete_pdu(s, vs->pdu, err);
3266
    v9fs_string_free(&vs->name);
3267
    qemu_free(vs);
3268
}
3269

    
3270
static void v9fs_post_lxattr_getvalue(V9fsState *s,
3271
                                      V9fsXattrState *vs, int err)
3272
{
3273
    if (err < 0) {
3274
        err = -errno;
3275
        free_fid(s, vs->xattr_fidp->fid);
3276
        goto out;
3277
    }
3278
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3279
    err = vs->offset;
3280
out:
3281
    complete_pdu(s, vs->pdu, err);
3282
    v9fs_string_free(&vs->name);
3283
    qemu_free(vs);
3284
    return;
3285
}
3286

    
3287
static void v9fs_post_lxattr_check(V9fsState *s,
3288
                                   V9fsXattrState *vs, ssize_t err)
3289
{
3290
    if (err < 0) {
3291
        err = -errno;
3292
        free_fid(s, vs->xattr_fidp->fid);
3293
        goto out;
3294
    }
3295
    /*
3296
     * Read the xattr value
3297
     */
3298
    vs->xattr_fidp->fs.xattr.len = vs->size;
3299
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3300
    vs->xattr_fidp->fs.xattr.copied_len = -1;
3301
    if (vs->size) {
3302
        vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3303
        err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3304
                                 vs->xattr_fidp->fs.xattr.value,
3305
                                 vs->xattr_fidp->fs.xattr.len);
3306
    }
3307
    v9fs_post_lxattr_getvalue(s, vs, err);
3308
    return;
3309
out:
3310
    complete_pdu(s, vs->pdu, err);
3311
    v9fs_string_free(&vs->name);
3312
    qemu_free(vs);
3313
}
3314

    
3315
static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3316
{
3317
    ssize_t err = 0;
3318
    V9fsXattrState *vs;
3319
    int32_t fid, newfid;
3320

    
3321
    vs = qemu_malloc(sizeof(*vs));
3322
    vs->pdu = pdu;
3323
    vs->offset = 7;
3324

    
3325
    pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3326
    vs->file_fidp = lookup_fid(s, fid);
3327
    if (vs->file_fidp == NULL) {
3328
        err = -ENOENT;
3329
        goto out;
3330
    }
3331

    
3332
    vs->xattr_fidp = alloc_fid(s, newfid);
3333
    if (vs->xattr_fidp == NULL) {
3334
        err = -EINVAL;
3335
        goto out;
3336
    }
3337

    
3338
    v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3339
    if (vs->name.data[0] == 0) {
3340
        /*
3341
         * listxattr request. Get the size first
3342
         */
3343
        vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3344
                                      NULL, 0);
3345
        if (vs->size < 0) {
3346
            err = vs->size;
3347
        }
3348
        v9fs_post_lxattr_check(s, vs, err);
3349
        return;
3350
    } else {
3351
        /*
3352
         * specific xattr fid. We check for xattr
3353
         * presence also collect the xattr size
3354
         */
3355
        vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3356
                                     &vs->name, NULL, 0);
3357
        if (vs->size < 0) {
3358
            err = vs->size;
3359
        }
3360
        v9fs_post_xattr_check(s, vs, err);
3361
        return;
3362
    }
3363
out:
3364
    complete_pdu(s, vs->pdu, err);
3365
    v9fs_string_free(&vs->name);
3366
    qemu_free(vs);
3367
}
3368

    
3369
static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3370
{
3371
    int flags;
3372
    int32_t fid;
3373
    ssize_t err = 0;
3374
    V9fsXattrState *vs;
3375

    
3376
    vs = qemu_malloc(sizeof(*vs));
3377
    vs->pdu = pdu;
3378
    vs->offset = 7;
3379

    
3380
    pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3381
                  &fid, &vs->name, &vs->size, &flags);
3382

    
3383
    vs->file_fidp = lookup_fid(s, fid);
3384
    if (vs->file_fidp == NULL) {
3385
        err = -EINVAL;
3386
        goto out;
3387
    }
3388

    
3389
    /* Make the file fid point to xattr */
3390
    vs->xattr_fidp = vs->file_fidp;
3391
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3392
    vs->xattr_fidp->fs.xattr.copied_len = 0;
3393
    vs->xattr_fidp->fs.xattr.len = vs->size;
3394
    vs->xattr_fidp->fs.xattr.flags = flags;
3395
    v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3396
    v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3397
    if (vs->size)
3398
        vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3399
    else
3400
        vs->xattr_fidp->fs.xattr.value = NULL;
3401

    
3402
out:
3403
    complete_pdu(s, vs->pdu, err);
3404
    v9fs_string_free(&vs->name);
3405
    qemu_free(vs);
3406
}
3407

    
3408
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3409

    
3410
static pdu_handler_t *pdu_handlers[] = {
3411
    [P9_TREADDIR] = v9fs_readdir,
3412
    [P9_TSTATFS] = v9fs_statfs,
3413
    [P9_TGETATTR] = v9fs_getattr,
3414
    [P9_TSETATTR] = v9fs_setattr,
3415
    [P9_TXATTRWALK] = v9fs_xattrwalk,
3416
    [P9_TXATTRCREATE] = v9fs_xattrcreate,
3417
    [P9_TMKNOD] = v9fs_mknod,
3418
    [P9_TRENAME] = v9fs_rename,
3419
    [P9_TMKDIR] = v9fs_mkdir,
3420
    [P9_TVERSION] = v9fs_version,
3421
    [P9_TLOPEN] = v9fs_open,
3422
    [P9_TATTACH] = v9fs_attach,
3423
    [P9_TSTAT] = v9fs_stat,
3424
    [P9_TWALK] = v9fs_walk,
3425
    [P9_TCLUNK] = v9fs_clunk,
3426
    [P9_TOPEN] = v9fs_open,
3427
    [P9_TREAD] = v9fs_read,
3428
#if 0
3429
    [P9_TAUTH] = v9fs_auth,
3430
#endif
3431
    [P9_TFLUSH] = v9fs_flush,
3432
    [P9_TLINK] = v9fs_link,
3433
    [P9_TSYMLINK] = v9fs_symlink,
3434
    [P9_TCREATE] = v9fs_create,
3435
    [P9_TLCREATE] = v9fs_lcreate,
3436
    [P9_TWRITE] = v9fs_write,
3437
    [P9_TWSTAT] = v9fs_wstat,
3438
    [P9_TREMOVE] = v9fs_remove,
3439
};
3440

    
3441
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3442
{
3443
    pdu_handler_t *handler;
3444

    
3445
    if (debug_9p_pdu) {
3446
        pprint_pdu(pdu);
3447
    }
3448

    
3449
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
3450

    
3451
    handler = pdu_handlers[pdu->id];
3452
    BUG_ON(handler == NULL);
3453

    
3454
    handler(s, pdu);
3455
}
3456

    
3457
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3458
{
3459
    V9fsState *s = (V9fsState *)vdev;
3460
    V9fsPDU *pdu;
3461
    ssize_t len;
3462

    
3463
    while ((pdu = alloc_pdu(s)) &&
3464
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3465
        uint8_t *ptr;
3466

    
3467
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3468
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3469

    
3470
        ptr = pdu->elem.out_sg[0].iov_base;
3471

    
3472
        memcpy(&pdu->size, ptr, 4);
3473
        pdu->id = ptr[4];
3474
        memcpy(&pdu->tag, ptr + 5, 2);
3475

    
3476
        submit_pdu(s, pdu);
3477
    }
3478

    
3479
    free_pdu(s, pdu);
3480
}
3481

    
3482
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3483
{
3484
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
3485
    return features;
3486
}
3487

    
3488
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3489
{
3490
    return (V9fsState *)vdev;
3491
}
3492

    
3493
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
3494
{
3495
    struct virtio_9p_config *cfg;
3496
    V9fsState *s = to_virtio_9p(vdev);
3497

    
3498
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
3499
                        s->tag_len);
3500
    stw_raw(&cfg->tag_len, s->tag_len);
3501
    memcpy(cfg->tag, s->tag, s->tag_len);
3502
    memcpy(config, cfg, s->config_size);
3503
    qemu_free(cfg);
3504
}
3505

    
3506
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
3507
 {
3508
    V9fsState *s;
3509
    int i, len;
3510
    struct stat stat;
3511
    FsTypeEntry *fse;
3512

    
3513

    
3514
    s = (V9fsState *)virtio_common_init("virtio-9p",
3515
                                    VIRTIO_ID_9P,
3516
                                    sizeof(struct virtio_9p_config)+
3517
                                    MAX_TAG_LEN,
3518
                                    sizeof(V9fsState));
3519

    
3520
    /* initialize pdu allocator */
3521
    QLIST_INIT(&s->free_list);
3522
    for (i = 0; i < (MAX_REQ - 1); i++) {
3523
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3524
    }
3525

    
3526
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
3527

    
3528
    fse = get_fsdev_fsentry(conf->fsdev_id);
3529

    
3530
    if (!fse) {
3531
        /* We don't have a fsdev identified by fsdev_id */
3532
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
3533
                    "with the id %s\n", conf->fsdev_id);
3534
        exit(1);
3535
    }
3536

    
3537
    if (!fse->path || !conf->tag) {
3538
        /* we haven't specified a mount_tag or the path */
3539
        fprintf(stderr, "fsdev with id %s needs path "
3540
                "and Virtio-9p device needs mount_tag arguments\n",
3541
                conf->fsdev_id);
3542
        exit(1);
3543
    }
3544

    
3545
    if (!strcmp(fse->security_model, "passthrough")) {
3546
        /* Files on the Fileserver set to client user credentials */
3547
        s->ctx.fs_sm = SM_PASSTHROUGH;
3548
    } else if (!strcmp(fse->security_model, "mapped")) {
3549
        /* Files on the fileserver are set to QEMU credentials.
3550
         * Client user credentials are saved in extended attributes.
3551
         */
3552
        s->ctx.fs_sm = SM_MAPPED;
3553
    } else if (!strcmp(fse->security_model, "none")) {
3554
        /*
3555
         * Files on the fileserver are set to QEMU credentials.
3556
         */
3557
        s->ctx.fs_sm = SM_NONE;
3558

    
3559
    } else {
3560
        fprintf(stderr, "Default to security_model=none. You may want"
3561
                " enable advanced security model using "
3562
                "security option:\n\t security_model=passthrough \n\t "
3563
                "security_model=mapped\n");
3564
        s->ctx.fs_sm = SM_NONE;
3565
    }
3566

    
3567
    if (lstat(fse->path, &stat)) {
3568
        fprintf(stderr, "share path %s does not exist\n", fse->path);
3569
        exit(1);
3570
    } else if (!S_ISDIR(stat.st_mode)) {
3571
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
3572
        exit(1);
3573
    }
3574

    
3575
    s->ctx.fs_root = qemu_strdup(fse->path);
3576
    len = strlen(conf->tag);
3577
    if (len > MAX_TAG_LEN) {
3578
        len = MAX_TAG_LEN;
3579
    }
3580
    /* s->tag is non-NULL terminated string */
3581
    s->tag = qemu_malloc(len);
3582
    memcpy(s->tag, conf->tag, len);
3583
    s->tag_len = len;
3584
    s->ctx.uid = -1;
3585

    
3586
    s->ops = fse->ops;
3587
    s->vdev.get_features = virtio_9p_get_features;
3588
    s->config_size = sizeof(struct virtio_9p_config) +
3589
                        s->tag_len;
3590
    s->vdev.get_config = virtio_9p_get_config;
3591

    
3592
    return &s->vdev;
3593
}