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

P
Peter Maydell 已提交
14
#include "qemu/osdep.h"
15
#include <glib/gprintf.h>
P
Paolo Bonzini 已提交
16
#include "hw/virtio/virtio.h"
17
#include "qapi/error.h"
18
#include "qemu/error-report.h"
M
Michael S. Tsirkin 已提交
19
#include "qemu/iov.h"
20
#include "qemu/sockets.h"
21 22
#include "virtio-9p.h"
#include "fsdev/qemu-fsdev.h"
23
#include "9p-xattr.h"
24
#include "coth.h"
25
#include "trace.h"
26
#include "migration/migration.h"
27

28 29 30
int open_fd_hw;
int total_open_fd;
static int open_fd_rc;
31

32 33 34 35 36 37 38 39 40 41 42 43
enum {
    Oread   = 0x00,
    Owrite  = 0x01,
    Ordwr   = 0x02,
    Oexec   = 0x03,
    Oexcl   = 0x04,
    Otrunc  = 0x10,
    Orexec  = 0x20,
    Orclose = 0x40,
    Oappend = 0x80,
};

44 45 46 47 48 49
ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
    ssize_t ret;
    va_list ap;

    va_start(ap, fmt);
50
    ret = virtio_pdu_vmarshal(pdu, offset, fmt, ap);
51 52 53 54 55 56 57 58 59 60 61
    va_end(ap);

    return ret;
}

ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
    ssize_t ret;
    va_list ap;

    va_start(ap, fmt);
62
    ret = virtio_pdu_vunmarshal(pdu, offset, fmt, ap);
63 64 65 66 67
    va_end(ap);

    return ret;
}

W
Wei Liu 已提交
68 69
static void pdu_push_and_notify(V9fsPDU *pdu)
{
70
    virtio_9p_push_and_notify(pdu);
W
Wei Liu 已提交
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
static int omode_to_uflags(int8_t mode)
{
    int ret = 0;

    switch (mode & 3) {
    case Oread:
        ret = O_RDONLY;
        break;
    case Ordwr:
        ret = O_RDWR;
        break;
    case Owrite:
        ret = O_WRONLY;
        break;
    case Oexec:
        ret = O_RDONLY;
        break;
    }

    if (mode & Otrunc) {
        ret |= O_TRUNC;
    }

    if (mode & Oappend) {
        ret |= O_APPEND;
    }

    if (mode & Oexcl) {
        ret |= O_EXCL;
    }

    return ret;
}

M
M. Mohan Kumar 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
struct dotl_openflag_map {
    int dotl_flag;
    int open_flag;
};

static int dotl_to_open_flags(int flags)
{
    int i;
    /*
     * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
     * and P9_DOTL_NOACCESS
     */
    int oflags = flags & O_ACCMODE;

    struct dotl_openflag_map dotl_oflag_map[] = {
        { P9_DOTL_CREATE, O_CREAT },
        { P9_DOTL_EXCL, O_EXCL },
        { P9_DOTL_NOCTTY , O_NOCTTY },
        { P9_DOTL_TRUNC, O_TRUNC },
        { P9_DOTL_APPEND, O_APPEND },
        { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
        { P9_DOTL_DSYNC, O_DSYNC },
        { P9_DOTL_FASYNC, FASYNC },
        { P9_DOTL_DIRECT, O_DIRECT },
        { P9_DOTL_LARGEFILE, O_LARGEFILE },
        { P9_DOTL_DIRECTORY, O_DIRECTORY },
        { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
        { P9_DOTL_NOATIME, O_NOATIME },
        { P9_DOTL_SYNC, O_SYNC },
    };

    for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
        if (flags & dotl_oflag_map[i].dotl_flag) {
            oflags |= dotl_oflag_map[i].open_flag;
        }
    }

    return oflags;
}

147
void cred_init(FsCred *credp)
148
{
149 150 151 152
    credp->fc_uid = -1;
    credp->fc_gid = -1;
    credp->fc_mode = -1;
    credp->fc_rdev = -1;
153 154
}

155 156 157 158 159 160
static int get_dotl_openflags(V9fsState *s, int oflags)
{
    int flags;
    /*
     * Filter the client open flags
     */
M
M. Mohan Kumar 已提交
161 162
    flags = dotl_to_open_flags(oflags);
    flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
163 164 165 166 167 168 169
    /*
     * Ignore direct disk access hint until the server supports it.
     */
    flags &= ~O_DIRECT;
    return flags;
}

170 171 172 173 174 175 176 177 178 179 180 181 182
void v9fs_path_init(V9fsPath *path)
{
    path->data = NULL;
    path->size = 0;
}

void v9fs_path_free(V9fsPath *path)
{
    g_free(path->data);
    path->data = NULL;
    path->size = 0;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196

void GCC_FMT_ATTR(2, 3)
v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
{
    va_list ap;

    v9fs_path_free(path);

    va_start(ap, fmt);
    /* Bump the size for including terminating NULL */
    path->size = g_vasprintf(&path->data, fmt, ap) + 1;
    va_end(ap);
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
{
    v9fs_path_free(lhs);
    lhs->data = g_malloc(rhs->size);
    memcpy(lhs->data, rhs->data, rhs->size);
    lhs->size = rhs->size;
}

int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
                      const char *name, V9fsPath *path)
{
    int err;
    err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
    if (err < 0) {
        err = -errno;
    }
    return err;
}

216 217 218 219 220 221
/*
 * Return TRUE if s1 is an ancestor of s2.
 *
 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
 * As a special case, We treat s1 as ancestor of s2 if they are same!
 */
222
static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
223
{
224 225
    if (!strncmp(s1->data, s2->data, s1->size - 1)) {
        if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
226 227 228 229 230 231
            return 1;
        }
    }
    return 0;
}

232 233 234 235 236
static size_t v9fs_string_size(V9fsString *str)
{
    return str->size;
}

237 238
/*
 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
239
static int v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
240 241 242 243 244
{
    int err = 1;
    if (f->fid_type == P9_FID_FILE) {
        if (f->fs.fd == -1) {
            do {
245 246
                err = v9fs_co_open(pdu, f, f->open_flags);
            } while (err == -EINTR && !pdu->cancelled);
247 248
        }
    } else if (f->fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
249
        if (f->fs.dir.stream == NULL) {
250
            do {
251 252
                err = v9fs_co_opendir(pdu, f);
            } while (err == -EINTR && !pdu->cancelled);
253 254 255 256 257
        }
    }
    return err;
}

258
static V9fsFidState *get_fid(V9fsPDU *pdu, int32_t fid)
259
{
260
    int err;
261
    V9fsFidState *f;
262
    V9fsState *s = pdu->s;
263 264

    for (f = s->fid_list; f; f = f->next) {
265
        BUG_ON(f->clunked);
266
        if (f->fid == fid) {
267 268 269 270 271
            /*
             * Update the fid ref upfront so that
             * we don't get reclaimed when we yield
             * in open later.
             */
272
            f->ref++;
273 274 275 276 277 278
            /*
             * check whether we need to reopen the
             * file. We might have closed the fd
             * while trying to free up some file
             * descriptors.
             */
279
            err = v9fs_reopen_fid(pdu, f);
280 281 282 283
            if (err < 0) {
                f->ref--;
                return NULL;
            }
284 285 286 287 288
            /*
             * Mark the fid as referenced so that the LRU
             * reclaim won't close the file descriptor
             */
            f->flags |= FID_REFERENCED;
289 290 291 292 293 294 295 296 297 298
            return f;
        }
    }
    return NULL;
}

static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
{
    V9fsFidState *f;

299 300 301 302 303 304
    for (f = s->fid_list; f; f = f->next) {
        /* If fid is already there return NULL */
        BUG_ON(f->clunked);
        if (f->fid == fid) {
            return NULL;
        }
305
    }
306
    f = g_malloc0(sizeof(V9fsFidState));
307
    f->fid = fid;
308
    f->fid_type = P9_FID_NONE;
309
    f->ref = 1;
310 311 312 313 314
    /*
     * Mark the fid as referenced so that the LRU
     * reclaim won't close the file descriptor
     */
    f->flags |= FID_REFERENCED;
315 316 317
    f->next = s->fid_list;
    s->fid_list = f;

G
Greg Kurz 已提交
318 319 320
    v9fs_readdir_init(&f->fs.dir);
    v9fs_readdir_init(&f->fs_reclaim.dir);

321 322 323
    return f;
}

324
static int v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
{
    int retval = 0;

    if (fidp->fs.xattr.copied_len == -1) {
        /* getxattr/listxattr fid */
        goto free_value;
    }
    /*
     * if this is fid for setxattr. clunk should
     * result in setxattr localcall
     */
    if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
        /* clunk after partial write */
        retval = -EINVAL;
        goto free_out;
    }
341
    if (fidp->fs.xattr.len) {
342
        retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
343 344 345 346
                                   fidp->fs.xattr.value,
                                   fidp->fs.xattr.len,
                                   fidp->fs.xattr.flags);
    } else {
347
        retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
348
    }
349 350 351
free_out:
    v9fs_string_free(&fidp->fs.xattr.name);
free_value:
352
    g_free(fidp->fs.xattr.value);
353 354 355
    return retval;
}

356
static int free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
357
{
358
    int retval = 0;
359 360

    if (fidp->fid_type == P9_FID_FILE) {
361 362
        /* If we reclaimed the fd no need to close */
        if (fidp->fs.fd != -1) {
363
            retval = v9fs_co_close(pdu, &fidp->fs);
364
        }
365
    } else if (fidp->fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
366
        if (fidp->fs.dir.stream != NULL) {
367
            retval = v9fs_co_closedir(pdu, &fidp->fs);
368
        }
369
    } else if (fidp->fid_type == P9_FID_XATTR) {
370
        retval = v9fs_xattr_fid_clunk(pdu, fidp);
371
    }
372
    v9fs_path_free(&fidp->path);
373 374 375 376
    g_free(fidp);
    return retval;
}

377
static int put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
378 379 380
{
    BUG_ON(!fidp->ref);
    fidp->ref--;
381 382 383
    /*
     * Don't free the fid if it is in reclaim list
     */
384
    if (!fidp->ref && fidp->clunked) {
385 386 387 388 389 390 391 392 393 394 395 396 397
        if (fidp->fid == pdu->s->root_fid) {
            /*
             * if the clunked fid is root fid then we
             * have unmounted the fs on the client side.
             * delete the migration blocker. Ideally, this
             * should be hooked to transport close notification
             */
            if (pdu->s->migration_blocker) {
                migrate_del_blocker(pdu->s->migration_blocker);
                error_free(pdu->s->migration_blocker);
                pdu->s->migration_blocker = NULL;
            }
        }
398
        return free_fid(pdu, fidp);
399
    }
400
    return 0;
401 402
}

403
static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
404
{
405 406 407 408 409 410 411 412
    V9fsFidState **fidpp, *fidp;

    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
        if ((*fidpp)->fid == fid) {
            break;
        }
    }
    if (*fidpp == NULL) {
413
        return NULL;
414 415 416
    }
    fidp = *fidpp;
    *fidpp = fidp->next;
417
    fidp->clunked = 1;
418
    return fidp;
419 420
}

421
void v9fs_reclaim_fd(V9fsPDU *pdu)
422 423
{
    int reclaim_count = 0;
424
    V9fsState *s = pdu->s;
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    V9fsFidState *f, *reclaim_list = NULL;

    for (f = s->fid_list; f; f = f->next) {
        /*
         * Unlink fids cannot be reclaimed. Check
         * for them and skip them. Also skip fids
         * currently being operated on.
         */
        if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
            continue;
        }
        /*
         * if it is a recently referenced fid
         * we leave the fid untouched and clear the
         * reference bit. We come back to it later
         * in the next iteration. (a simple LRU without
         * moving list elements around)
         */
        if (f->flags & FID_REFERENCED) {
            f->flags &= ~FID_REFERENCED;
            continue;
        }
        /*
         * Add fids to reclaim list.
         */
        if (f->fid_type == P9_FID_FILE) {
            if (f->fs.fd != -1) {
                /*
                 * Up the reference count so that
                 * a clunk request won't free this fid
                 */
                f->ref++;
                f->rclm_lst = reclaim_list;
                reclaim_list = f;
                f->fs_reclaim.fd = f->fs.fd;
                f->fs.fd = -1;
                reclaim_count++;
            }
463
        } else if (f->fid_type == P9_FID_DIR) {
G
Greg Kurz 已提交
464
            if (f->fs.dir.stream != NULL) {
465 466 467 468 469 470 471
                /*
                 * Up the reference count so that
                 * a clunk request won't free this fid
                 */
                f->ref++;
                f->rclm_lst = reclaim_list;
                reclaim_list = f;
G
Greg Kurz 已提交
472 473
                f->fs_reclaim.dir.stream = f->fs.dir.stream;
                f->fs.dir.stream = NULL;
474 475
                reclaim_count++;
            }
476 477 478 479 480 481 482 483 484 485 486 487 488
        }
        if (reclaim_count >= open_fd_rc) {
            break;
        }
    }
    /*
     * Now close the fid in reclaim list. Free them if they
     * are already clunked.
     */
    while (reclaim_list) {
        f = reclaim_list;
        reclaim_list = f->rclm_lst;
        if (f->fid_type == P9_FID_FILE) {
489
            v9fs_co_close(pdu, &f->fs_reclaim);
490
        } else if (f->fid_type == P9_FID_DIR) {
491
            v9fs_co_closedir(pdu, &f->fs_reclaim);
492 493 494 495 496 497
        }
        f->rclm_lst = NULL;
        /*
         * Now drop the fid reference, free it
         * if clunked.
         */
498
        put_fid(pdu, f);
499 500 501
    }
}

502
static int v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
503 504
{
    int err;
505
    V9fsState *s = pdu->s;
506 507 508 509
    V9fsFidState *fidp, head_fid;

    head_fid.next = s->fid_list;
    for (fidp = s->fid_list; fidp; fidp = fidp->next) {
510 511 512 513
        if (fidp->path.size != path->size) {
            continue;
        }
        if (!memcmp(fidp->path.data, path->data, path->size)) {
514 515
            /* Mark the fid non reclaimable. */
            fidp->flags |= FID_NON_RECLAIMABLE;
516 517

            /* reopen the file/dir if already closed */
518
            err = v9fs_reopen_fid(pdu, fidp);
519 520 521 522 523 524 525 526 527
            if (err < 0) {
                return -1;
            }
            /*
             * Go back to head of fid list because
             * the list could have got updated when
             * switched to the worker thread
             */
            if (err == 0) {
528 529 530 531 532 533 534
                fidp = &head_fid;
            }
        }
    }
    return 0;
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
static void virtfs_reset(V9fsPDU *pdu)
{
    V9fsState *s = pdu->s;
    V9fsFidState *fidp = NULL;

    /* Free all fids */
    while (s->fid_list) {
        fidp = s->fid_list;
        s->fid_list = fidp->next;

        if (fidp->ref) {
            fidp->clunked = 1;
        } else {
            free_fid(pdu, fidp);
        }
    }
    if (fidp) {
        /* One or more unclunked fids found... */
        error_report("9pfs:%s: One or more uncluncked fids "
                     "found during reset", __func__);
    }
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
#define P9_QID_TYPE_DIR         0x80
#define P9_QID_TYPE_SYMLINK     0x02

#define P9_STAT_MODE_DIR        0x80000000
#define P9_STAT_MODE_APPEND     0x40000000
#define P9_STAT_MODE_EXCL       0x20000000
#define P9_STAT_MODE_MOUNT      0x10000000
#define P9_STAT_MODE_AUTH       0x08000000
#define P9_STAT_MODE_TMP        0x04000000
#define P9_STAT_MODE_SYMLINK    0x02000000
#define P9_STAT_MODE_LINK       0x01000000
#define P9_STAT_MODE_DEVICE     0x00800000
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
#define P9_STAT_MODE_SOCKET     0x00100000
#define P9_STAT_MODE_SETUID     0x00080000
#define P9_STAT_MODE_SETGID     0x00040000
#define P9_STAT_MODE_SETVTX     0x00010000

#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
                                P9_STAT_MODE_SYMLINK |      \
                                P9_STAT_MODE_LINK |         \
                                P9_STAT_MODE_DEVICE |       \
                                P9_STAT_MODE_NAMED_PIPE |   \
                                P9_STAT_MODE_SOCKET)

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

588
    memset(&qidp->path, 0, sizeof(qidp->path));
589 590 591 592 593 594 595 596 597 598 599 600
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
    memcpy(&qidp->path, &stbuf->st_ino, size);
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
    qidp->type = 0;
    if (S_ISDIR(stbuf->st_mode)) {
        qidp->type |= P9_QID_TYPE_DIR;
    }
    if (S_ISLNK(stbuf->st_mode)) {
        qidp->type |= P9_QID_TYPE_SYMLINK;
    }
}

601
static int fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, V9fsQID *qidp)
602 603 604 605
{
    struct stat stbuf;
    int err;

606
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
607
    if (err < 0) {
608 609 610 611 612 613
        return err;
    }
    stat_to_qid(&stbuf, qidp);
    return 0;
}

W
Wei Liu 已提交
614
V9fsPDU *pdu_alloc(V9fsState *s)
615 616 617 618
{
    V9fsPDU *pdu = NULL;

    if (!QLIST_EMPTY(&s->free_list)) {
619 620 621
        pdu = QLIST_FIRST(&s->free_list);
        QLIST_REMOVE(pdu, next);
        QLIST_INSERT_HEAD(&s->active_list, pdu, next);
622 623 624 625
    }
    return pdu;
}

W
Wei Liu 已提交
626
void pdu_free(V9fsPDU *pdu)
627 628
{
    if (pdu) {
629
        V9fsState *s = pdu->s;
630 631 632 633 634 635 636 637
        /*
         * Cancelled pdu are added back to the freelist
         * by flush request .
         */
        if (!pdu->cancelled) {
            QLIST_REMOVE(pdu, next);
            QLIST_INSERT_HEAD(&s->free_list, pdu, next);
        }
638 639 640
    }
}

641 642 643 644 645
/*
 * We don't do error checking for pdu_marshal/unmarshal here
 * because we always expect to have enough space to encode
 * error details
 */
646
static void pdu_complete(V9fsPDU *pdu, ssize_t len)
647 648
{
    int8_t id = pdu->id + 1; /* Response */
649
    V9fsState *s = pdu->s;
650 651 652

    if (len < 0) {
        int err = -len;
653
        len = 7;
654

655 656 657 658 659 660 661 662 663
        if (s->proto_version != V9FS_PROTO_2000L) {
            V9fsString str;

            str.data = strerror(err);
            str.size = strlen(str.data);

            len += pdu_marshal(pdu, len, "s", &str);
            id = P9_RERROR;
        }
664

665
        len += pdu_marshal(pdu, len, "d", err);
666

667 668 669
        if (s->proto_version == V9FS_PROTO_2000L) {
            id = P9_RLERROR;
        }
670
        trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
671 672 673 674 675 676 677 678 679
    }

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

    /* keep these in sync */
    pdu->size = len;
    pdu->id = id;

W
Wei Liu 已提交
680
    pdu_push_and_notify(pdu);
681

682 683 684
    /* Now wakeup anybody waiting in flush for this request */
    qemu_co_queue_next(&pdu->complete);

685
    pdu_free(pdu);
686 687
}

688 689 690 691 692 693 694 695 696
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
{
    mode_t ret;

    ret = mode & 0777;
    if (mode & P9_STAT_MODE_DIR) {
        ret |= S_IFDIR;
    }

697 698 699 700 701 702 703 704 705 706
    if (mode & P9_STAT_MODE_SYMLINK) {
        ret |= S_IFLNK;
    }
    if (mode & P9_STAT_MODE_SOCKET) {
        ret |= S_IFSOCK;
    }
    if (mode & P9_STAT_MODE_NAMED_PIPE) {
        ret |= S_IFIFO;
    }
    if (mode & P9_STAT_MODE_DEVICE) {
707
        if (extension->size && extension->data[0] == 'c') {
708 709 710
            ret |= S_IFCHR;
        } else {
            ret |= S_IFBLK;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        }
    }

    if (!(ret&~0777)) {
        ret |= S_IFREG;
    }

    if (mode & P9_STAT_MODE_SETUID) {
        ret |= S_ISUID;
    }
    if (mode & P9_STAT_MODE_SETGID) {
        ret |= S_ISGID;
    }
    if (mode & P9_STAT_MODE_SETVTX) {
        ret |= S_ISVTX;
    }

    return ret;
}

static int donttouch_stat(V9fsStat *stat)
{
    if (stat->type == -1 &&
        stat->dev == -1 &&
        stat->qid.type == -1 &&
        stat->qid.version == -1 &&
        stat->qid.path == -1 &&
        stat->mode == -1 &&
        stat->atime == -1 &&
        stat->mtime == -1 &&
        stat->length == -1 &&
        !stat->name.size &&
        !stat->uid.size &&
        !stat->gid.size &&
        !stat->muid.size &&
        stat->n_uid == -1 &&
        stat->n_gid == -1 &&
        stat->n_muid == -1) {
        return 1;
    }

    return 0;
}

755 756 757 758 759 760 761 762 763
static void v9fs_stat_init(V9fsStat *stat)
{
    v9fs_string_init(&stat->name);
    v9fs_string_init(&stat->uid);
    v9fs_string_init(&stat->gid);
    v9fs_string_init(&stat->muid);
    v9fs_string_init(&stat->extension);
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
static void v9fs_stat_free(V9fsStat *stat)
{
    v9fs_string_free(&stat->name);
    v9fs_string_free(&stat->uid);
    v9fs_string_free(&stat->gid);
    v9fs_string_free(&stat->muid);
    v9fs_string_free(&stat->extension);
}

static uint32_t stat_to_v9mode(const struct stat *stbuf)
{
    uint32_t mode;

    mode = stbuf->st_mode & 0777;
    if (S_ISDIR(stbuf->st_mode)) {
        mode |= P9_STAT_MODE_DIR;
    }

782 783 784
    if (S_ISLNK(stbuf->st_mode)) {
        mode |= P9_STAT_MODE_SYMLINK;
    }
785

786 787 788
    if (S_ISSOCK(stbuf->st_mode)) {
        mode |= P9_STAT_MODE_SOCKET;
    }
789

790 791 792
    if (S_ISFIFO(stbuf->st_mode)) {
        mode |= P9_STAT_MODE_NAMED_PIPE;
    }
793

794 795 796
    if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
        mode |= P9_STAT_MODE_DEVICE;
    }
797

798 799 800
    if (stbuf->st_mode & S_ISUID) {
        mode |= P9_STAT_MODE_SETUID;
    }
801

802 803 804
    if (stbuf->st_mode & S_ISGID) {
        mode |= P9_STAT_MODE_SETGID;
    }
805

806 807
    if (stbuf->st_mode & S_ISVTX) {
        mode |= P9_STAT_MODE_SETVTX;
808 809 810 811 812
    }

    return mode;
}

813
static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
814 815 816 817 818 819 820 821 822 823 824 825 826 827
                            const struct stat *stbuf,
                            V9fsStat *v9stat)
{
    int err;
    const char *str;

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

    stat_to_qid(stbuf, &v9stat->qid);
    v9stat->mode = stat_to_v9mode(stbuf);
    v9stat->atime = stbuf->st_atime;
    v9stat->mtime = stbuf->st_mtime;
    v9stat->length = stbuf->st_size;

828 829 830
    v9fs_string_free(&v9stat->uid);
    v9fs_string_free(&v9stat->gid);
    v9fs_string_free(&v9stat->muid);
831

832 833 834
    v9stat->n_uid = stbuf->st_uid;
    v9stat->n_gid = stbuf->st_gid;
    v9stat->n_muid = 0;
835

836
    v9fs_string_free(&v9stat->extension);
837

838
    if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
839
        err = v9fs_co_readlink(pdu, name, &v9stat->extension);
840
        if (err < 0) {
841
            return err;
842
        }
843 844 845 846 847
    } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
        v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
                S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
                major(stbuf->st_rdev), minor(stbuf->st_rdev));
    } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
848 849
        v9fs_string_sprintf(&v9stat->extension, "%s %lu",
                "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    }

    str = strrchr(name->data, '/');
    if (str) {
        str += 1;
    } else {
        str = name->data;
    }

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

    v9stat->size = 61 +
        v9fs_string_size(&v9stat->name) +
        v9fs_string_size(&v9stat->uid) +
        v9fs_string_size(&v9stat->gid) +
        v9fs_string_size(&v9stat->muid) +
        v9fs_string_size(&v9stat->extension);
    return 0;
}

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
#define P9_STATS_MODE          0x00000001ULL
#define P9_STATS_NLINK         0x00000002ULL
#define P9_STATS_UID           0x00000004ULL
#define P9_STATS_GID           0x00000008ULL
#define P9_STATS_RDEV          0x00000010ULL
#define P9_STATS_ATIME         0x00000020ULL
#define P9_STATS_MTIME         0x00000040ULL
#define P9_STATS_CTIME         0x00000080ULL
#define P9_STATS_INO           0x00000100ULL
#define P9_STATS_SIZE          0x00000200ULL
#define P9_STATS_BLOCKS        0x00000400ULL

#define P9_STATS_BTIME         0x00000800ULL
#define P9_STATS_GEN           0x00001000ULL
#define P9_STATS_DATA_VERSION  0x00002000ULL

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


static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
891
                                V9fsStatDotl *v9lstat)
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
{
    memset(v9lstat, 0, sizeof(*v9lstat));

    v9lstat->st_mode = stbuf->st_mode;
    v9lstat->st_nlink = stbuf->st_nlink;
    v9lstat->st_uid = stbuf->st_uid;
    v9lstat->st_gid = stbuf->st_gid;
    v9lstat->st_rdev = stbuf->st_rdev;
    v9lstat->st_size = stbuf->st_size;
    v9lstat->st_blksize = stbuf->st_blksize;
    v9lstat->st_blocks = stbuf->st_blocks;
    v9lstat->st_atime_sec = stbuf->st_atime;
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
    v9lstat->st_mtime_sec = stbuf->st_mtime;
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
    v9lstat->st_ctime_sec = stbuf->st_ctime;
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
    /* Currently we only support BASIC fields in stat */
    v9lstat->st_result_mask = P9_STATS_BASIC;

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

915 916 917 918 919 920 921 922 923 924 925 926 927 928
static void print_sg(struct iovec *sg, int cnt)
{
    int i;

    printf("sg[%d]: {", cnt);
    for (i = 0; i < cnt; i++) {
        if (i) {
            printf(", ");
        }
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
    }
    printf("}\n");
}

929 930
/* Will call this only for path name based fid */
static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
931
{
932 933 934
    V9fsPath str;
    v9fs_path_init(&str);
    v9fs_path_copy(&str, dst);
935
    v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
936
    v9fs_path_free(&str);
937 938
}

939 940 941 942 943
static inline bool is_ro_export(FsContext *ctx)
{
    return ctx->export_flags & V9FS_RDONLY;
}

944
static void v9fs_version(void *opaque)
945
{
946
    ssize_t err;
947 948
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
949 950 951
    V9fsString version;
    size_t offset = 7;

952 953 954 955 956 957
    v9fs_string_init(&version);
    err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
    if (err < 0) {
        offset = err;
        goto out;
    }
958
    trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
959

960 961
    virtfs_reset(pdu);

962 963 964 965 966
    if (!strcmp(version.data, "9P2000.u")) {
        s->proto_version = V9FS_PROTO_2000U;
    } else if (!strcmp(version.data, "9P2000.L")) {
        s->proto_version = V9FS_PROTO_2000L;
    } else {
967
        v9fs_string_sprintf(&version, "unknown");
968
    }
969

970 971 972 973 974 975
    err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
    if (err < 0) {
        offset = err;
        goto out;
    }
    offset += err;
976
    trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
977
out:
978
    pdu_complete(pdu, offset);
979
    v9fs_string_free(&version);
980 981
}

982
static void v9fs_attach(void *opaque)
983
{
984 985
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
986 987 988 989
    int32_t fid, afid, n_uname;
    V9fsString uname, aname;
    V9fsFidState *fidp;
    size_t offset = 7;
990
    V9fsQID qid;
991 992
    ssize_t err;

993 994 995 996 997 998 999
    v9fs_string_init(&uname);
    v9fs_string_init(&aname);
    err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
                        &afid, &uname, &aname, &n_uname);
    if (err < 0) {
        goto out_nofid;
    }
1000
    trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1001 1002 1003 1004

    fidp = alloc_fid(s, fid);
    if (fidp == NULL) {
        err = -EINVAL;
1005
        goto out_nofid;
1006
    }
1007
    fidp->uid = n_uname;
1008
    err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1009 1010 1011 1012 1013
    if (err < 0) {
        err = -EINVAL;
        clunk_fid(s, fid);
        goto out;
    }
1014
    err = fid_to_qid(pdu, fidp, &qid);
1015
    if (err < 0) {
1016
        err = -EINVAL;
1017
        clunk_fid(s, fid);
1018 1019
        goto out;
    }
1020 1021 1022 1023 1024 1025
    err = pdu_marshal(pdu, offset, "Q", &qid);
    if (err < 0) {
        clunk_fid(s, fid);
        goto out;
    }
    err += offset;
1026
    memcpy(&s->root_qid, &qid, sizeof(qid));
1027 1028
    trace_v9fs_attach_return(pdu->tag, pdu->id,
                             qid.type, qid.version, qid.path);
1029 1030 1031 1032 1033 1034
    /*
     * disable migration if we haven't done already.
     * attach could get called multiple times for the same export.
     */
    if (!s->migration_blocker) {
        s->root_fid = fid;
1035 1036 1037
        error_setg(&s->migration_blocker,
                   "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                   s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1038 1039
        migrate_add_blocker(s->migration_blocker);
    }
1040
out:
1041
    put_fid(pdu, fidp);
1042
out_nofid:
1043
    pdu_complete(pdu, err);
1044 1045
    v9fs_string_free(&uname);
    v9fs_string_free(&aname);
1046 1047
}

1048
static void v9fs_stat(void *opaque)
1049
{
1050
    int32_t fid;
1051
    V9fsStat v9stat;
1052
    ssize_t err = 0;
1053 1054 1055 1056
    size_t offset = 7;
    struct stat stbuf;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
1057

1058 1059 1060 1061
    err = pdu_unmarshal(pdu, offset, "d", &fid);
    if (err < 0) {
        goto out_nofid;
    }
1062
    trace_v9fs_stat(pdu->tag, pdu->id, fid);
1063

1064
    fidp = get_fid(pdu, fid);
1065
    if (fidp == NULL) {
1066
        err = -ENOENT;
1067
        goto out_nofid;
1068
    }
1069
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1070 1071 1072
    if (err < 0) {
        goto out;
    }
1073
    err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1074 1075 1076
    if (err < 0) {
        goto out;
    }
1077 1078 1079 1080 1081
    err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
    if (err < 0) {
        v9fs_stat_free(&v9stat);
        goto out;
    }
1082 1083
    trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
                           v9stat.atime, v9stat.mtime, v9stat.length);
1084
    err += offset;
1085
    v9fs_stat_free(&v9stat);
1086
out:
1087
    put_fid(pdu, fidp);
1088
out_nofid:
1089
    pdu_complete(pdu, err);
1090 1091
}

1092
static void v9fs_getattr(void *opaque)
1093 1094
{
    int32_t fid;
1095 1096 1097
    size_t offset = 7;
    ssize_t retval = 0;
    struct stat stbuf;
1098 1099
    V9fsFidState *fidp;
    uint64_t request_mask;
1100 1101 1102
    V9fsStatDotl v9stat_dotl;
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
1103

1104 1105 1106 1107
    retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
    if (retval < 0) {
        goto out_nofid;
    }
1108
    trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1109

1110
    fidp = get_fid(pdu, fid);
1111
    if (fidp == NULL) {
1112
        retval = -ENOENT;
1113
        goto out_nofid;
1114
    }
1115 1116
    /*
     * Currently we only support BASIC fields in stat, so there is no
1117 1118
     * need to look at request_mask.
     */
1119
    retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1120 1121 1122 1123
    if (retval < 0) {
        goto out;
    }
    stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1124 1125 1126 1127

    /*  fill st_gen if requested and supported by underlying fs */
    if (request_mask & P9_STATS_GEN) {
        retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1128 1129 1130 1131 1132 1133 1134
        switch (retval) {
        case 0:
            /* we have valid st_gen: update result mask */
            v9stat_dotl.st_result_mask |= P9_STATS_GEN;
            break;
        case -EINTR:
            /* request cancelled, e.g. by Tflush */
1135
            goto out;
1136 1137 1138
        default:
            /* failed to get st_gen: not fatal, ignore */
            break;
1139 1140
        }
    }
1141 1142 1143 1144 1145
    retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
    if (retval < 0) {
        goto out;
    }
    retval += offset;
1146 1147 1148
    trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
                              v9stat_dotl.st_mode, v9stat_dotl.st_uid,
                              v9stat_dotl.st_gid);
1149 1150 1151
out:
    put_fid(pdu, fidp);
out_nofid:
1152
    pdu_complete(pdu, retval);
1153 1154
}

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
/* Attribute flags */
#define P9_ATTR_MODE       (1 << 0)
#define P9_ATTR_UID        (1 << 1)
#define P9_ATTR_GID        (1 << 2)
#define P9_ATTR_SIZE       (1 << 3)
#define P9_ATTR_ATIME      (1 << 4)
#define P9_ATTR_MTIME      (1 << 5)
#define P9_ATTR_CTIME      (1 << 6)
#define P9_ATTR_ATIME_SET  (1 << 7)
#define P9_ATTR_MTIME_SET  (1 << 8)

#define P9_ATTR_MASK    127
1167

1168
static void v9fs_setattr(void *opaque)
1169
{
1170 1171 1172 1173 1174 1175
    int err = 0;
    int32_t fid;
    V9fsFidState *fidp;
    size_t offset = 7;
    V9fsIattr v9iattr;
    V9fsPDU *pdu = opaque;
1176

1177 1178 1179 1180
    err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
    if (err < 0) {
        goto out_nofid;
    }
1181

1182
    fidp = get_fid(pdu, fid);
1183 1184
    if (fidp == NULL) {
        err = -EINVAL;
1185
        goto out_nofid;
1186
    }
1187
    if (v9iattr.valid & P9_ATTR_MODE) {
1188
        err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1189 1190
        if (err < 0) {
            goto out;
1191 1192
        }
    }
1193
    if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1194
        struct timespec times[2];
1195 1196
        if (v9iattr.valid & P9_ATTR_ATIME) {
            if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1197 1198
                times[0].tv_sec = v9iattr.atime_sec;
                times[0].tv_nsec = v9iattr.atime_nsec;
1199 1200 1201 1202 1203 1204
            } else {
                times[0].tv_nsec = UTIME_NOW;
            }
        } else {
            times[0].tv_nsec = UTIME_OMIT;
        }
1205 1206
        if (v9iattr.valid & P9_ATTR_MTIME) {
            if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1207 1208
                times[1].tv_sec = v9iattr.mtime_sec;
                times[1].tv_nsec = v9iattr.mtime_nsec;
1209 1210 1211 1212 1213 1214
            } else {
                times[1].tv_nsec = UTIME_NOW;
            }
        } else {
            times[1].tv_nsec = UTIME_OMIT;
        }
1215
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
1216 1217 1218
        if (err < 0) {
            goto out;
        }
1219
    }
1220 1221 1222 1223
    /*
     * If the only valid entry in iattr is ctime we can call
     * chown(-1,-1) to update the ctime of the file
     */
1224 1225 1226 1227
    if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
        ((v9iattr.valid & P9_ATTR_CTIME)
         && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
        if (!(v9iattr.valid & P9_ATTR_UID)) {
1228 1229
            v9iattr.uid = -1;
        }
1230
        if (!(v9iattr.valid & P9_ATTR_GID)) {
1231 1232
            v9iattr.gid = -1;
        }
1233
        err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1234 1235 1236 1237
                            v9iattr.gid);
        if (err < 0) {
            goto out;
        }
1238
    }
1239
    if (v9iattr.valid & (P9_ATTR_SIZE)) {
1240
        err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1241 1242 1243
        if (err < 0) {
            goto out;
        }
1244
    }
1245
    err = offset;
1246
out:
1247
    put_fid(pdu, fidp);
1248
out_nofid:
1249
    pdu_complete(pdu, err);
1250 1251
}

1252
static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1253 1254
{
    int i;
1255
    ssize_t err;
1256
    size_t offset = 7;
1257 1258 1259 1260 1261 1262

    err = pdu_marshal(pdu, offset, "w", nwnames);
    if (err < 0) {
        return err;
    }
    offset += err;
1263
    for (i = 0; i < nwnames; i++) {
1264 1265 1266 1267 1268
        err = pdu_marshal(pdu, offset, "Q", &qids[i]);
        if (err < 0) {
            return err;
        }
        offset += err;
1269
    }
1270
    return offset;
1271 1272
}

G
Greg Kurz 已提交
1273 1274 1275 1276 1277
static bool name_is_illegal(const char *name)
{
    return !*name || strchr(name, '/') != NULL;
}

1278 1279 1280 1281 1282 1283 1284 1285
static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
{
    return
        qid1->type != qid2->type ||
        qid1->version != qid2->version ||
        qid1->path != qid2->path;
}

1286
static void v9fs_walk(void *opaque)
1287
{
1288 1289 1290
    int name_idx;
    V9fsQID *qids = NULL;
    int i, err = 0;
1291
    V9fsPath dpath, path;
1292 1293 1294 1295 1296 1297
    uint16_t nwnames;
    struct stat stbuf;
    size_t offset = 7;
    int32_t fid, newfid;
    V9fsString *wnames = NULL;
    V9fsFidState *fidp;
1298
    V9fsFidState *newfidp = NULL;
1299 1300
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
1301
    V9fsQID qid;
1302

1303 1304
    err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
    if (err < 0) {
1305
        pdu_complete(pdu, err);
1306 1307 1308
        return ;
    }
    offset += err;
1309

1310 1311
    trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);

1312 1313 1314 1315
    if (nwnames && nwnames <= P9_MAXWELEM) {
        wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
        for (i = 0; i < nwnames; i++) {
1316 1317 1318 1319
            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
            if (err < 0) {
                goto out_nofid;
            }
G
Greg Kurz 已提交
1320 1321 1322 1323
            if (name_is_illegal(wnames[i].data)) {
                err = -ENOENT;
                goto out_nofid;
            }
1324
            offset += err;
1325
        }
1326
    } else if (nwnames > P9_MAXWELEM) {
1327
        err = -EINVAL;
1328
        goto out_nofid;
1329
    }
1330
    fidp = get_fid(pdu, fid);
1331
    if (fidp == NULL) {
1332
        err = -ENOENT;
1333
        goto out_nofid;
1334
    }
1335

1336 1337 1338
    v9fs_path_init(&dpath);
    v9fs_path_init(&path);

1339 1340 1341 1342 1343
    err = fid_to_qid(pdu, fidp, &qid);
    if (err < 0) {
        goto out;
    }

1344 1345 1346 1347 1348 1349 1350
    /*
     * Both dpath and path initially poin to fidp.
     * Needed to handle request with nwnames == 0
     */
    v9fs_path_copy(&dpath, &fidp->path);
    v9fs_path_copy(&path, &fidp->path);
    for (name_idx = 0; name_idx < nwnames; name_idx++) {
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
        if (not_same_qid(&pdu->s->root_qid, &qid) ||
            strcmp("..", wnames[name_idx].data)) {
            err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
                                       &path);
            if (err < 0) {
                goto out;
            }

            err = v9fs_co_lstat(pdu, &path, &stbuf);
            if (err < 0) {
                goto out;
            }
            stat_to_qid(&stbuf, &qid);
            v9fs_path_copy(&dpath, &path);
1365
        }
1366
        memcpy(&qids[name_idx], &qid, sizeof(qid));
1367
    }
1368
    if (fid == newfid) {
1369
        BUG_ON(fidp->fid_type != P9_FID_NONE);
1370
        v9fs_path_copy(&fidp->path, &path);
1371
    } else {
1372 1373
        newfidp = alloc_fid(s, newfid);
        if (newfidp == NULL) {
1374 1375 1376
            err = -EINVAL;
            goto out;
        }
1377
        newfidp->uid = fidp->uid;
1378
        v9fs_path_copy(&newfidp->path, &path);
1379
    }
1380
    err = v9fs_walk_marshal(pdu, nwnames, qids);
1381
    trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1382
out:
1383
    put_fid(pdu, fidp);
1384
    if (newfidp) {
1385
        put_fid(pdu, newfidp);
1386
    }
1387 1388
    v9fs_path_free(&dpath);
    v9fs_path_free(&path);
1389
out_nofid:
1390
    pdu_complete(pdu, err);
1391 1392 1393 1394 1395 1396 1397
    if (nwnames && nwnames <= P9_MAXWELEM) {
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
            v9fs_string_free(&wnames[name_idx]);
        }
        g_free(wnames);
        g_free(qids);
    }
1398 1399
}

1400
static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1401 1402 1403
{
    struct statfs stbuf;
    int32_t iounit = 0;
1404
    V9fsState *s = pdu->s;
1405 1406 1407 1408 1409

    /*
     * iounit should be multiples of f_bsize (host filesystem block size
     * and as well as less than (client msize - P9_IOHDRSZ))
     */
1410
    if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1411 1412 1413 1414 1415 1416 1417 1418 1419
        iounit = stbuf.f_bsize;
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
    }
    if (!iounit) {
        iounit = s->msize - P9_IOHDRSZ;
    }
    return iounit;
}

1420
static void v9fs_open(void *opaque)
1421
{
1422 1423 1424 1425
    int flags;
    int32_t fid;
    int32_t mode;
    V9fsQID qid;
1426
    int iounit = 0;
1427 1428 1429 1430 1431 1432
    ssize_t err = 0;
    size_t offset = 7;
    struct stat stbuf;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
1433

1434
    if (s->proto_version == V9FS_PROTO_2000L) {
1435
        err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1436
    } else {
1437 1438 1439
        uint8_t modebyte;
        err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
        mode = modebyte;
1440 1441 1442
    }
    if (err < 0) {
        goto out_nofid;
1443
    }
1444 1445
    trace_v9fs_open(pdu->tag, pdu->id, fid, mode);

1446
    fidp = get_fid(pdu, fid);
1447 1448
    if (fidp == NULL) {
        err = -ENOENT;
1449
        goto out_nofid;
1450
    }
1451
    BUG_ON(fidp->fid_type != P9_FID_NONE);
1452

1453
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1454
    if (err < 0) {
1455 1456
        goto out;
    }
1457 1458
    stat_to_qid(&stbuf, &qid);
    if (S_ISDIR(stbuf.st_mode)) {
1459
        err = v9fs_co_opendir(pdu, fidp);
1460 1461 1462 1463
        if (err < 0) {
            goto out;
        }
        fidp->fid_type = P9_FID_DIR;
1464 1465 1466 1467 1468
        err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
        if (err < 0) {
            goto out;
        }
        err += offset;
1469
    } else {
1470
        if (s->proto_version == V9FS_PROTO_2000L) {
1471
            flags = get_dotl_openflags(s, mode);
1472
        } else {
1473
            flags = omode_to_uflags(mode);
1474
        }
1475 1476 1477 1478 1479 1480 1481
        if (is_ro_export(&s->ctx)) {
            if (mode & O_WRONLY || mode & O_RDWR ||
                mode & O_APPEND || mode & O_TRUNC) {
                err = -EROFS;
                goto out;
            }
        }
1482
        err = v9fs_co_open(pdu, fidp, flags);
1483 1484 1485 1486
        if (err < 0) {
            goto out;
        }
        fidp->fid_type = P9_FID_FILE;
1487 1488 1489 1490 1491 1492 1493 1494
        fidp->open_flags = flags;
        if (flags & O_EXCL) {
            /*
             * We let the host file system do O_EXCL check
             * We should not reclaim such fd
             */
            fidp->flags |= FID_NON_RECLAIMABLE;
        }
1495
        iounit = get_iounit(pdu, &fidp->path);
1496 1497 1498 1499 1500
        err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
        if (err < 0) {
            goto out;
        }
        err += offset;
1501
    }
1502 1503
    trace_v9fs_open_return(pdu->tag, pdu->id,
                           qid.type, qid.version, qid.path, iounit);
1504
out:
1505
    put_fid(pdu, fidp);
1506
out_nofid:
1507
    pdu_complete(pdu, err);
1508 1509
}

1510
static void v9fs_lcreate(void *opaque)
1511 1512 1513 1514
{
    int32_t dfid, flags, mode;
    gid_t gid;
    ssize_t err = 0;
1515 1516 1517 1518 1519 1520 1521
    ssize_t offset = 7;
    V9fsString name;
    V9fsFidState *fidp;
    struct stat stbuf;
    V9fsQID qid;
    int32_t iounit;
    V9fsPDU *pdu = opaque;
1522

1523 1524 1525 1526 1527 1528
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
                        &name, &flags, &mode, &gid);
    if (err < 0) {
        goto out_nofid;
    }
1529
    trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1530

G
Greg Kurz 已提交
1531 1532 1533 1534 1535
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
1536 1537 1538 1539 1540
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

1541
    fidp = get_fid(pdu, dfid);
1542
    if (fidp == NULL) {
1543
        err = -ENOENT;
1544
        goto out_nofid;
1545 1546
    }

1547
    flags = get_dotl_openflags(pdu->s, flags);
1548
    err = v9fs_co_open2(pdu, fidp, &name, gid,
1549
                        flags | O_CREAT, mode, &stbuf);
1550 1551 1552 1553
    if (err < 0) {
        goto out;
    }
    fidp->fid_type = P9_FID_FILE;
1554 1555 1556 1557 1558 1559 1560 1561
    fidp->open_flags = flags;
    if (flags & O_EXCL) {
        /*
         * We let the host file system do O_EXCL check
         * We should not reclaim such fd
         */
        fidp->flags |= FID_NON_RECLAIMABLE;
    }
1562
    iounit =  get_iounit(pdu, &fidp->path);
1563
    stat_to_qid(&stbuf, &qid);
1564 1565 1566 1567 1568
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
    if (err < 0) {
        goto out;
    }
    err += offset;
1569 1570
    trace_v9fs_lcreate_return(pdu->tag, pdu->id,
                              qid.type, qid.version, qid.path, iounit);
1571
out:
1572
    put_fid(pdu, fidp);
1573
out_nofid:
1574
    pdu_complete(pdu, err);
1575
    v9fs_string_free(&name);
1576 1577
}

1578
static void v9fs_fsync(void *opaque)
1579
{
1580
    int err;
1581
    int32_t fid;
1582
    int datasync;
1583 1584
    size_t offset = 7;
    V9fsFidState *fidp;
1585
    V9fsPDU *pdu = opaque;
1586

1587 1588 1589 1590
    err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
    if (err < 0) {
        goto out_nofid;
    }
1591 1592
    trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);

1593
    fidp = get_fid(pdu, fid);
1594 1595
    if (fidp == NULL) {
        err = -ENOENT;
1596
        goto out_nofid;
1597
    }
1598
    err = v9fs_co_fsync(pdu, fidp, datasync);
1599 1600 1601
    if (!err) {
        err = offset;
    }
1602
    put_fid(pdu, fidp);
1603
out_nofid:
1604
    pdu_complete(pdu, err);
1605 1606
}

1607
static void v9fs_clunk(void *opaque)
1608
{
1609
    int err;
1610 1611
    int32_t fid;
    size_t offset = 7;
1612
    V9fsFidState *fidp;
1613 1614
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
1615

1616 1617 1618 1619
    err = pdu_unmarshal(pdu, offset, "d", &fid);
    if (err < 0) {
        goto out_nofid;
    }
1620
    trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1621

1622
    fidp = clunk_fid(s, fid);
1623 1624 1625 1626
    if (fidp == NULL) {
        err = -ENOENT;
        goto out_nofid;
    }
1627 1628 1629 1630 1631
    /*
     * Bump the ref so that put_fid will
     * free the fid.
     */
    fidp->ref++;
1632 1633 1634 1635
    err = put_fid(pdu, fidp);
    if (!err) {
        err = offset;
    }
1636
out_nofid:
1637
    pdu_complete(pdu, err);
1638 1639
}

1640 1641
static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
                           uint64_t off, uint32_t max_count)
1642
{
1643
    ssize_t err;
1644 1645 1646
    size_t offset = 7;
    int read_count;
    int64_t xattr_len;
W
Wei Liu 已提交
1647
    V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1648
    VirtQueueElement *elem = v->elems[pdu->idx];
1649

1650 1651 1652 1653 1654 1655 1656 1657 1658
    xattr_len = fidp->fs.xattr.len;
    read_count = xattr_len - off;
    if (read_count > max_count) {
        read_count = max_count;
    } else if (read_count < 0) {
        /*
         * read beyond XATTR value
         */
        read_count = 0;
1659
    }
1660 1661 1662 1663 1664
    err = pdu_marshal(pdu, offset, "d", read_count);
    if (err < 0) {
        return err;
    }
    offset += err;
W
Wei Liu 已提交
1665 1666

    err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1667 1668 1669 1670 1671 1672
                    ((char *)fidp->fs.xattr.value) + off,
                    read_count);
    if (err < 0) {
        return err;
    }
    offset += err;
1673
    return offset;
1674 1675
}

1676
static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1677
                                     V9fsFidState *fidp, uint32_t max_count)
1678
{
1679
    V9fsPath path;
1680 1681 1682 1683 1684
    V9fsStat v9stat;
    int len, err = 0;
    int32_t count = 0;
    struct stat stbuf;
    off_t saved_dir_pos;
G
Greg Kurz 已提交
1685
    struct dirent *dent;
1686

1687
    /* save the directory position */
1688
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1689 1690
    if (saved_dir_pos < 0) {
        return saved_dir_pos;
1691
    }
1692

1693
    while (1) {
1694
        v9fs_path_init(&path);
G
Greg Kurz 已提交
1695 1696 1697

        v9fs_readdir_lock(&fidp->fs.dir);

G
Greg Kurz 已提交
1698 1699
        err = v9fs_co_readdir(pdu, fidp, &dent);
        if (err || !dent) {
1700 1701
            break;
        }
1702
        err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1703
        if (err < 0) {
G
Greg Kurz 已提交
1704
            break;
1705
        }
1706
        err = v9fs_co_lstat(pdu, &path, &stbuf);
1707
        if (err < 0) {
G
Greg Kurz 已提交
1708
            break;
1709
        }
1710
        err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1711
        if (err < 0) {
G
Greg Kurz 已提交
1712
            break;
1713
        }
1714 1715
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
        len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
G
Greg Kurz 已提交
1716 1717 1718

        v9fs_readdir_unlock(&fidp->fs.dir);

1719 1720
        if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
            /* Ran out of buffer. Set dir back to old position and return */
1721
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1722
            v9fs_stat_free(&v9stat);
1723
            v9fs_path_free(&path);
1724 1725 1726 1727
            return count;
        }
        count += len;
        v9fs_stat_free(&v9stat);
1728
        v9fs_path_free(&path);
1729
        saved_dir_pos = dent->d_off;
1730
    }
G
Greg Kurz 已提交
1731

G
Greg Kurz 已提交
1732 1733
    v9fs_readdir_unlock(&fidp->fs.dir);

1734
    v9fs_path_free(&path);
1735 1736
    if (err < 0) {
        return err;
1737
    }
1738
    return count;
1739 1740
}

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
/*
 * Create a QEMUIOVector for a sub-region of PDU iovecs
 *
 * @qiov:       uninitialized QEMUIOVector
 * @skip:       number of bytes to skip from beginning of PDU
 * @size:       number of bytes to include
 * @is_write:   true - write, false - read
 *
 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
 * with qemu_iovec_destroy().
 */
static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1753
                                    size_t skip, size_t size,
1754 1755 1756 1757 1758 1759
                                    bool is_write)
{
    QEMUIOVector elem;
    struct iovec *iov;
    unsigned int niov;

1760
    virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1761 1762 1763

    qemu_iovec_init_external(&elem, iov, niov);
    qemu_iovec_init(qiov, niov);
1764
    qemu_iovec_concat(qiov, &elem, skip, size);
1765 1766
}

1767
static void v9fs_read(void *opaque)
1768
{
1769
    int32_t fid;
1770
    uint64_t off;
1771
    ssize_t err = 0;
1772 1773
    int32_t count = 0;
    size_t offset = 7;
1774
    uint32_t max_count;
1775 1776 1777
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
1778

1779 1780 1781 1782
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
    if (err < 0) {
        goto out_nofid;
    }
1783
    trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1784

1785
    fidp = get_fid(pdu, fid);
1786
    if (fidp == NULL) {
1787
        err = -EINVAL;
1788
        goto out_nofid;
1789
    }
1790
    if (fidp->fid_type == P9_FID_DIR) {
1791

1792
        if (off == 0) {
1793
            v9fs_co_rewinddir(pdu, fidp);
1794
        }
1795
        count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1796 1797 1798
        if (count < 0) {
            err = count;
            goto out;
1799
        }
1800 1801 1802 1803 1804
        err = pdu_marshal(pdu, offset, "d", count);
        if (err < 0) {
            goto out;
        }
        err += offset + count;
1805
    } else if (fidp->fid_type == P9_FID_FILE) {
1806 1807
        QEMUIOVector qiov_full;
        QEMUIOVector qiov;
1808 1809
        int32_t len;

1810 1811
        v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
        qemu_iovec_init(&qiov, qiov_full.niov);
1812
        do {
1813
            qemu_iovec_reset(&qiov);
1814
            qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1815
            if (0) {
1816
                print_sg(qiov.iov, qiov.niov);
1817 1818 1819
            }
            /* Loop in case of EINTR */
            do {
1820
                len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1821 1822 1823 1824
                if (len >= 0) {
                    off   += len;
                    count += len;
                }
1825
            } while (len == -EINTR && !pdu->cancelled);
1826 1827 1828 1829 1830 1831
            if (len < 0) {
                /* IO error return the error */
                err = len;
                goto out;
            }
        } while (count < max_count && len > 0);
1832 1833 1834 1835 1836
        err = pdu_marshal(pdu, offset, "d", count);
        if (err < 0) {
            goto out;
        }
        err += offset + count;
1837 1838
        qemu_iovec_destroy(&qiov);
        qemu_iovec_destroy(&qiov_full);
1839 1840
    } else if (fidp->fid_type == P9_FID_XATTR) {
        err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1841 1842
    } else {
        err = -EINVAL;
1843
    }
1844
    trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1845
out:
1846
    put_fid(pdu, fidp);
1847
out_nofid:
1848
    pdu_complete(pdu, err);
1849 1850
}

1851
static size_t v9fs_readdir_data_size(V9fsString *name)
1852
{
1853 1854 1855 1856 1857
    /*
     * Size of each dirent on the wire: size of qid (13) + size of offset (8)
     * size of type (1) + size of name.size (2) + strlen(name.data)
     */
    return 24 + v9fs_string_size(name);
1858 1859
}

1860
static int v9fs_do_readdir(V9fsPDU *pdu,
1861
                           V9fsFidState *fidp, int32_t max_count)
1862 1863
{
    size_t size;
1864 1865 1866 1867 1868
    V9fsQID qid;
    V9fsString name;
    int len, err = 0;
    int32_t count = 0;
    off_t saved_dir_pos;
G
Greg Kurz 已提交
1869
    struct dirent *dent;
1870

1871
    /* save the directory position */
1872
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1873 1874 1875
    if (saved_dir_pos < 0) {
        return saved_dir_pos;
    }
1876

1877
    while (1) {
G
Greg Kurz 已提交
1878 1879
        v9fs_readdir_lock(&fidp->fs.dir);

G
Greg Kurz 已提交
1880 1881
        err = v9fs_co_readdir(pdu, fidp, &dent);
        if (err || !dent) {
1882 1883 1884 1885 1886
            break;
        }
        v9fs_string_init(&name);
        v9fs_string_sprintf(&name, "%s", dent->d_name);
        if ((count + v9fs_readdir_data_size(&name)) > max_count) {
G
Greg Kurz 已提交
1887 1888
            v9fs_readdir_unlock(&fidp->fs.dir);

1889
            /* Ran out of buffer. Set dir back to old position and return */
1890
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1891 1892
            v9fs_string_free(&name);
            return count;
1893
        }
1894 1895
        /*
         * Fill up just the path field of qid because the client uses
1896 1897 1898
         * only that. To fill the entire qid structure we will have
         * to stat each dirent found, which is expensive
         */
1899 1900
        size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
        memcpy(&qid.path, &dent->d_ino, size);
1901
        /* Fill the other fields with dummy values */
1902 1903
        qid.type = 0;
        qid.version = 0;
1904

1905 1906 1907 1908
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
        len = pdu_marshal(pdu, 11 + count, "Qqbs",
                          &qid, dent->d_off,
                          dent->d_type, &name);
G
Greg Kurz 已提交
1909 1910 1911

        v9fs_readdir_unlock(&fidp->fs.dir);

1912 1913 1914 1915 1916
        if (len < 0) {
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
            v9fs_string_free(&name);
            return len;
        }
1917 1918 1919 1920
        count += len;
        v9fs_string_free(&name);
        saved_dir_pos = dent->d_off;
    }
G
Greg Kurz 已提交
1921 1922 1923

    v9fs_readdir_unlock(&fidp->fs.dir);

1924 1925 1926 1927
    if (err < 0) {
        return err;
    }
    return count;
1928 1929
}

1930
static void v9fs_readdir(void *opaque)
1931 1932
{
    int32_t fid;
1933 1934
    V9fsFidState *fidp;
    ssize_t retval = 0;
1935
    size_t offset = 7;
1936 1937 1938
    uint64_t initial_offset;
    int32_t count;
    uint32_t max_count;
1939
    V9fsPDU *pdu = opaque;
1940

1941 1942 1943 1944 1945
    retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
                           &initial_offset, &max_count);
    if (retval < 0) {
        goto out_nofid;
    }
1946 1947
    trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);

1948
    fidp = get_fid(pdu, fid);
1949 1950 1951 1952
    if (fidp == NULL) {
        retval = -EINVAL;
        goto out_nofid;
    }
G
Greg Kurz 已提交
1953
    if (!fidp->fs.dir.stream) {
1954
        retval = -EINVAL;
1955 1956
        goto out;
    }
1957
    if (initial_offset == 0) {
1958
        v9fs_co_rewinddir(pdu, fidp);
1959
    } else {
1960
        v9fs_co_seekdir(pdu, fidp, initial_offset);
1961
    }
1962
    count = v9fs_do_readdir(pdu, fidp, max_count);
1963 1964 1965 1966
    if (count < 0) {
        retval = count;
        goto out;
    }
1967 1968 1969 1970 1971
    retval = pdu_marshal(pdu, offset, "d", count);
    if (retval < 0) {
        goto out;
    }
    retval += count + offset;
1972
    trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1973
out:
1974
    put_fid(pdu, fidp);
1975
out_nofid:
1976
    pdu_complete(pdu, retval);
1977 1978
}

1979
static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1980
                            uint64_t off, uint32_t count,
1981
                            struct iovec *sg, int cnt)
1982 1983 1984 1985 1986
{
    int i, to_copy;
    ssize_t err = 0;
    int write_count;
    int64_t xattr_len;
1987
    size_t offset = 7;
1988

1989 1990 1991 1992 1993

    xattr_len = fidp->fs.xattr.len;
    write_count = xattr_len - off;
    if (write_count > count) {
        write_count = count;
1994 1995 1996 1997 1998 1999 2000 2001
    } else if (write_count < 0) {
        /*
         * write beyond XATTR value len specified in
         * xattrcreate
         */
        err = -ENOSPC;
        goto out;
    }
2002 2003 2004 2005 2006
    err = pdu_marshal(pdu, offset, "d", write_count);
    if (err < 0) {
        return err;
    }
    err += offset;
2007
    fidp->fs.xattr.copied_len += write_count;
2008 2009 2010
    /*
     * Now copy the content from sg list
     */
2011 2012 2013
    for (i = 0; i < cnt; i++) {
        if (write_count > sg[i].iov_len) {
            to_copy = sg[i].iov_len;
2014 2015 2016
        } else {
            to_copy = write_count;
        }
2017
        memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2018
        /* updating vs->off since we are not using below */
2019
        off += to_copy;
2020 2021 2022
        write_count -= to_copy;
    }
out:
2023
    return err;
2024 2025
}

2026
static void v9fs_write(void *opaque)
2027
{
2028 2029
    ssize_t err;
    int32_t fid;
2030 2031
    uint64_t off;
    uint32_t count;
2032 2033 2034 2035
    int32_t len = 0;
    int32_t total = 0;
    size_t offset = 7;
    V9fsFidState *fidp;
2036 2037
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
2038 2039
    QEMUIOVector qiov_full;
    QEMUIOVector qiov;
2040

2041 2042
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
    if (err < 0) {
2043
        pdu_complete(pdu, err);
S
Stefan Weil 已提交
2044
        return;
2045 2046
    }
    offset += err;
2047 2048
    v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
    trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2049

2050
    fidp = get_fid(pdu, fid);
2051
    if (fidp == NULL) {
2052
        err = -EINVAL;
2053
        goto out_nofid;
2054
    }
2055 2056
    if (fidp->fid_type == P9_FID_FILE) {
        if (fidp->fs.fd == -1) {
2057 2058 2059
            err = -EINVAL;
            goto out;
        }
2060
    } else if (fidp->fid_type == P9_FID_XATTR) {
2061 2062 2063
        /*
         * setxattr operation
         */
2064 2065
        err = v9fs_xattr_write(s, pdu, fidp, off, count,
                               qiov_full.iov, qiov_full.niov);
2066
        goto out;
2067
    } else {
2068 2069 2070
        err = -EINVAL;
        goto out;
    }
2071
    qemu_iovec_init(&qiov, qiov_full.niov);
2072
    do {
2073
        qemu_iovec_reset(&qiov);
2074
        qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2075
        if (0) {
2076
            print_sg(qiov.iov, qiov.niov);
2077
        }
2078 2079
        /* Loop in case of EINTR */
        do {
2080
            len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2081 2082 2083 2084
            if (len >= 0) {
                off   += len;
                total += len;
            }
2085
        } while (len == -EINTR && !pdu->cancelled);
2086 2087 2088
        if (len < 0) {
            /* IO error return the error */
            err = len;
2089
            goto out_qiov;
2090 2091
        }
    } while (total < count && len > 0);
2092 2093

    offset = 7;
2094 2095 2096 2097 2098
    err = pdu_marshal(pdu, offset, "d", total);
    if (err < 0) {
        goto out;
    }
    err += offset;
2099
    trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2100 2101
out_qiov:
    qemu_iovec_destroy(&qiov);
2102
out:
2103
    put_fid(pdu, fidp);
2104
out_nofid:
2105
    qemu_iovec_destroy(&qiov_full);
2106
    pdu_complete(pdu, err);
2107 2108
}

2109
static void v9fs_create(void *opaque)
2110
{
2111 2112 2113 2114 2115 2116 2117
    int32_t fid;
    int err = 0;
    size_t offset = 7;
    V9fsFidState *fidp;
    V9fsQID qid;
    int32_t perm;
    int8_t mode;
2118
    V9fsPath path;
2119 2120 2121 2122 2123
    struct stat stbuf;
    V9fsString name;
    V9fsString extension;
    int iounit;
    V9fsPDU *pdu = opaque;
2124

2125
    v9fs_path_init(&path);
2126 2127 2128 2129 2130 2131 2132
    v9fs_string_init(&name);
    v9fs_string_init(&extension);
    err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
                        &perm, &mode, &extension);
    if (err < 0) {
        goto out_nofid;
    }
2133 2134
    trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);

G
Greg Kurz 已提交
2135 2136 2137 2138 2139
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2140 2141 2142 2143 2144
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

2145
    fidp = get_fid(pdu, fid);
2146 2147
    if (fidp == NULL) {
        err = -EINVAL;
2148
        goto out_nofid;
2149
    }
2150
    if (perm & P9_STAT_MODE_DIR) {
2151
        err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2152
                            fidp->uid, -1, &stbuf);
2153 2154 2155
        if (err < 0) {
            goto out;
        }
2156
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2157 2158 2159 2160
        if (err < 0) {
            goto out;
        }
        v9fs_path_copy(&fidp->path, &path);
2161
        err = v9fs_co_opendir(pdu, fidp);
2162 2163 2164 2165 2166
        if (err < 0) {
            goto out;
        }
        fidp->fid_type = P9_FID_DIR;
    } else if (perm & P9_STAT_MODE_SYMLINK) {
2167
        err = v9fs_co_symlink(pdu, fidp, &name,
2168
                              extension.data, -1 , &stbuf);
2169 2170 2171
        if (err < 0) {
            goto out;
        }
2172
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2173 2174 2175 2176
        if (err < 0) {
            goto out;
        }
        v9fs_path_copy(&fidp->path, &path);
2177
    } else if (perm & P9_STAT_MODE_LINK) {
2178
        int32_t ofid = atoi(extension.data);
2179
        V9fsFidState *ofidp = get_fid(pdu, ofid);
2180
        if (ofidp == NULL) {
2181 2182 2183
            err = -EINVAL;
            goto out;
        }
2184 2185
        err = v9fs_co_link(pdu, ofidp, fidp, &name);
        put_fid(pdu, ofidp);
2186 2187 2188
        if (err < 0) {
            goto out;
        }
2189
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2190
        if (err < 0) {
2191
            fidp->fid_type = P9_FID_NONE;
2192
            goto out;
2193
        }
2194
        v9fs_path_copy(&fidp->path, &path);
2195
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2196 2197 2198 2199
        if (err < 0) {
            fidp->fid_type = P9_FID_NONE;
            goto out;
        }
2200
    } else if (perm & P9_STAT_MODE_DEVICE) {
2201 2202 2203 2204
        char ctype;
        uint32_t major, minor;
        mode_t nmode = 0;

2205
        if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2206
            err = -errno;
2207
            goto out;
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
        }

        switch (ctype) {
        case 'c':
            nmode = S_IFCHR;
            break;
        case 'b':
            nmode = S_IFBLK;
            break;
        default:
            err = -EIO;
2219 2220
            goto out;
        }
2221

2222
        nmode |= perm & 0777;
2223
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2224
                            makedev(major, minor), nmode, &stbuf);
2225 2226 2227
        if (err < 0) {
            goto out;
        }
2228
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2229 2230 2231 2232
        if (err < 0) {
            goto out;
        }
        v9fs_path_copy(&fidp->path, &path);
2233
    } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2234
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2235
                            0, S_IFIFO | (perm & 0777), &stbuf);
2236 2237 2238
        if (err < 0) {
            goto out;
        }
2239
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2240 2241 2242 2243
        if (err < 0) {
            goto out;
        }
        v9fs_path_copy(&fidp->path, &path);
2244
    } else if (perm & P9_STAT_MODE_SOCKET) {
2245
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2246
                            0, S_IFSOCK | (perm & 0777), &stbuf);
2247 2248 2249
        if (err < 0) {
            goto out;
        }
2250
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2251 2252 2253 2254
        if (err < 0) {
            goto out;
        }
        v9fs_path_copy(&fidp->path, &path);
2255
    } else {
2256
        err = v9fs_co_open2(pdu, fidp, &name, -1,
2257
                            omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2258 2259 2260 2261
        if (err < 0) {
            goto out;
        }
        fidp->fid_type = P9_FID_FILE;
2262 2263 2264 2265 2266 2267 2268 2269
        fidp->open_flags = omode_to_uflags(mode);
        if (fidp->open_flags & O_EXCL) {
            /*
             * We let the host file system do O_EXCL check
             * We should not reclaim such fd
             */
            fidp->flags |= FID_NON_RECLAIMABLE;
        }
2270
    }
2271
    iounit = get_iounit(pdu, &fidp->path);
2272
    stat_to_qid(&stbuf, &qid);
2273 2274 2275 2276 2277
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
    if (err < 0) {
        goto out;
    }
    err += offset;
2278 2279
    trace_v9fs_create_return(pdu->tag, pdu->id,
                             qid.type, qid.version, qid.path, iounit);
2280
out:
2281
    put_fid(pdu, fidp);
2282
out_nofid:
2283
   pdu_complete(pdu, err);
2284 2285
   v9fs_string_free(&name);
   v9fs_string_free(&extension);
2286
   v9fs_path_free(&path);
2287 2288
}

2289
static void v9fs_symlink(void *opaque)
2290
{
2291
    V9fsPDU *pdu = opaque;
2292 2293 2294 2295 2296
    V9fsString name;
    V9fsString symname;
    V9fsFidState *dfidp;
    V9fsQID qid;
    struct stat stbuf;
2297 2298 2299
    int32_t dfid;
    int err = 0;
    gid_t gid;
2300
    size_t offset = 7;
2301

2302 2303 2304 2305 2306 2307
    v9fs_string_init(&name);
    v9fs_string_init(&symname);
    err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
    if (err < 0) {
        goto out_nofid;
    }
2308
    trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2309

G
Greg Kurz 已提交
2310 2311 2312 2313 2314
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2315 2316 2317 2318 2319
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

2320
    dfidp = get_fid(pdu, dfid);
2321
    if (dfidp == NULL) {
2322
        err = -EINVAL;
2323
        goto out_nofid;
2324
    }
2325
    err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2326 2327 2328 2329
    if (err < 0) {
        goto out;
    }
    stat_to_qid(&stbuf, &qid);
2330 2331 2332 2333 2334
    err =  pdu_marshal(pdu, offset, "Q", &qid);
    if (err < 0) {
        goto out;
    }
    err += offset;
2335 2336
    trace_v9fs_symlink_return(pdu->tag, pdu->id,
                              qid.type, qid.version, qid.path);
2337
out:
2338
    put_fid(pdu, dfidp);
2339
out_nofid:
2340
    pdu_complete(pdu, err);
2341 2342
    v9fs_string_free(&name);
    v9fs_string_free(&symname);
2343 2344
}

2345
static void v9fs_flush(void *opaque)
2346
{
2347
    ssize_t err;
2348 2349 2350
    int16_t tag;
    size_t offset = 7;
    V9fsPDU *cancel_pdu;
2351 2352
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
2353

2354 2355
    err = pdu_unmarshal(pdu, offset, "w", &tag);
    if (err < 0) {
2356
        pdu_complete(pdu, err);
2357 2358
        return;
    }
2359
    trace_v9fs_flush(pdu->tag, pdu->id, tag);
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

    QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
        if (cancel_pdu->tag == tag) {
            break;
        }
    }
    if (cancel_pdu) {
        cancel_pdu->cancelled = 1;
        /*
         * Wait for pdu to complete.
         */
        qemu_co_queue_wait(&cancel_pdu->complete);
        cancel_pdu->cancelled = 0;
2373
        pdu_free(cancel_pdu);
2374
    }
2375
    pdu_complete(pdu, 7);
2376 2377
}

2378
static void v9fs_link(void *opaque)
2379
{
2380
    V9fsPDU *pdu = opaque;
2381 2382
    int32_t dfid, oldfid;
    V9fsFidState *dfidp, *oldfidp;
2383
    V9fsString name;
2384 2385 2386
    size_t offset = 7;
    int err = 0;

2387 2388 2389 2390 2391
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
    if (err < 0) {
        goto out_nofid;
    }
2392
    trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2393

G
Greg Kurz 已提交
2394 2395 2396 2397 2398
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2399 2400 2401 2402 2403
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

2404
    dfidp = get_fid(pdu, dfid);
2405
    if (dfidp == NULL) {
2406
        err = -ENOENT;
2407
        goto out_nofid;
2408 2409
    }

2410
    oldfidp = get_fid(pdu, oldfid);
2411
    if (oldfidp == NULL) {
2412
        err = -ENOENT;
2413 2414
        goto out;
    }
2415
    err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2416 2417
    if (!err) {
        err = offset;
2418 2419
    }
out:
2420
    put_fid(pdu, dfidp);
2421
out_nofid:
2422
    v9fs_string_free(&name);
2423
    pdu_complete(pdu, err);
2424 2425
}

2426
/* Only works with path name based fid */
2427
static void v9fs_remove(void *opaque)
2428
{
2429 2430
    int32_t fid;
    int err = 0;
2431 2432 2433
    size_t offset = 7;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
2434

2435 2436 2437 2438
    err = pdu_unmarshal(pdu, offset, "d", &fid);
    if (err < 0) {
        goto out_nofid;
    }
2439
    trace_v9fs_remove(pdu->tag, pdu->id, fid);
2440

2441
    fidp = get_fid(pdu, fid);
2442
    if (fidp == NULL) {
2443
        err = -EINVAL;
2444
        goto out_nofid;
2445
    }
2446
    /* if fs driver is not path based, return EOPNOTSUPP */
2447
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2448 2449 2450
        err = -EOPNOTSUPP;
        goto out_err;
    }
2451 2452 2453 2454
    /*
     * IF the file is unlinked, we cannot reopen
     * the file later. So don't reclaim fd
     */
2455
    err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2456 2457 2458
    if (err < 0) {
        goto out_err;
    }
2459
    err = v9fs_co_remove(pdu, &fidp->path);
2460 2461 2462
    if (!err) {
        err = offset;
    }
2463
out_err:
2464
    /* For TREMOVE we need to clunk the fid even on failed remove */
2465
    clunk_fid(pdu->s, fidp->fid);
2466
    put_fid(pdu, fidp);
2467
out_nofid:
2468
    pdu_complete(pdu, err);
2469 2470
}

2471 2472 2473 2474 2475 2476
static void v9fs_unlinkat(void *opaque)
{
    int err = 0;
    V9fsString name;
    int32_t dfid, flags;
    size_t offset = 7;
2477
    V9fsPath path;
2478 2479 2480
    V9fsFidState *dfidp;
    V9fsPDU *pdu = opaque;

2481 2482 2483 2484 2485
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
    if (err < 0) {
        goto out_nofid;
    }
G
Greg Kurz 已提交
2486 2487 2488 2489 2490 2491

    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
    if (!strcmp(".", name.data)) {
        err = -EINVAL;
        goto out_nofid;
    }

    if (!strcmp("..", name.data)) {
        err = -ENOTEMPTY;
        goto out_nofid;
    }

2502
    dfidp = get_fid(pdu, dfid);
2503 2504 2505 2506 2507 2508 2509 2510
    if (dfidp == NULL) {
        err = -EINVAL;
        goto out_nofid;
    }
    /*
     * IF the file is unlinked, we cannot reopen
     * the file later. So don't reclaim fd
     */
2511
    v9fs_path_init(&path);
2512
    err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2513 2514 2515
    if (err < 0) {
        goto out_err;
    }
2516
    err = v9fs_mark_fids_unreclaim(pdu, &path);
2517 2518 2519
    if (err < 0) {
        goto out_err;
    }
2520
    err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2521 2522 2523 2524
    if (!err) {
        err = offset;
    }
out_err:
2525
    put_fid(pdu, dfidp);
2526
    v9fs_path_free(&path);
2527
out_nofid:
2528
    pdu_complete(pdu, err);
2529 2530 2531
    v9fs_string_free(&name);
}

2532 2533

/* Only works with path name based fid */
2534
static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2535
                                int32_t newdirfid, V9fsString *name)
2536
{
2537
    char *end;
2538
    int err = 0;
2539 2540
    V9fsPath new_path;
    V9fsFidState *tfidp;
2541
    V9fsState *s = pdu->s;
2542
    V9fsFidState *dirfidp = NULL;
2543
    char *old_name, *new_name;
2544

2545
    v9fs_path_init(&new_path);
2546
    if (newdirfid != -1) {
2547
        dirfidp = get_fid(pdu, newdirfid);
2548 2549
        if (dirfidp == NULL) {
            err = -ENOENT;
2550
            goto out_nofid;
2551
        }
2552
        BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2553
        v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2554
    } else {
2555
        old_name = fidp->path.data;
2556 2557 2558 2559 2560 2561
        end = strrchr(old_name, '/');
        if (end) {
            end++;
        } else {
            end = old_name;
        }
2562
        new_name = g_malloc0(end - old_name + name->size + 1);
2563
        strncat(new_name, old_name, end - old_name);
2564
        strncat(new_name + (end - old_name), name->data, name->size);
2565
        v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2566
        g_free(new_name);
2567
    }
2568
    err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
    if (err < 0) {
        goto out;
    }
    /*
     * Fixup fid's pointing to the old name to
     * start pointing to the new name
     */
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
        if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
            /* replace the name */
            v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2580 2581
        }
    }
2582
out:
2583
    if (dirfidp) {
2584
        put_fid(pdu, dirfidp);
2585
    }
2586
    v9fs_path_free(&new_path);
2587
out_nofid:
2588 2589 2590
    return err;
}

2591
/* Only works with path name based fid */
2592
static void v9fs_rename(void *opaque)
2593 2594 2595
{
    int32_t fid;
    ssize_t err = 0;
2596 2597 2598 2599 2600 2601
    size_t offset = 7;
    V9fsString name;
    int32_t newdirfid;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
2602

2603 2604 2605 2606 2607
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
    if (err < 0) {
        goto out_nofid;
    }
G
Greg Kurz 已提交
2608 2609 2610 2611 2612 2613

    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2614 2615 2616 2617 2618
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EISDIR;
        goto out_nofid;
    }

2619
    fidp = get_fid(pdu, fid);
2620
    if (fidp == NULL) {
2621
        err = -ENOENT;
2622
        goto out_nofid;
2623
    }
2624
    BUG_ON(fidp->fid_type != P9_FID_NONE);
2625
    /* if fs driver is not path based, return EOPNOTSUPP */
2626
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2627 2628 2629 2630
        err = -EOPNOTSUPP;
        goto out;
    }
    v9fs_path_write_lock(s);
2631
    err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2632
    v9fs_path_unlock(s);
2633 2634 2635
    if (!err) {
        err = offset;
    }
2636
out:
2637
    put_fid(pdu, fidp);
2638
out_nofid:
2639
    pdu_complete(pdu, err);
2640
    v9fs_string_free(&name);
2641 2642
}

2643
static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2644 2645 2646 2647 2648
                               V9fsString *old_name, V9fsPath *newdir,
                               V9fsString *new_name)
{
    V9fsFidState *tfidp;
    V9fsPath oldpath, newpath;
2649
    V9fsState *s = pdu->s;
2650 2651 2652 2653


    v9fs_path_init(&oldpath);
    v9fs_path_init(&newpath);
2654 2655
    v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
    v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670

    /*
     * Fixup fid's pointing to the old name to
     * start pointing to the new name
     */
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
        if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
            /* replace the name */
            v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
        }
    }
    v9fs_path_free(&oldpath);
    v9fs_path_free(&newpath);
}

2671
static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2672 2673 2674 2675
                                  V9fsString *old_name, int32_t newdirfid,
                                  V9fsString *new_name)
{
    int err = 0;
2676
    V9fsState *s = pdu->s;
2677 2678
    V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;

2679
    olddirfidp = get_fid(pdu, olddirfid);
2680 2681 2682 2683 2684
    if (olddirfidp == NULL) {
        err = -ENOENT;
        goto out;
    }
    if (newdirfid != -1) {
2685
        newdirfidp = get_fid(pdu, newdirfid);
2686 2687 2688 2689 2690
        if (newdirfidp == NULL) {
            err = -ENOENT;
            goto out;
        }
    } else {
2691
        newdirfidp = get_fid(pdu, olddirfid);
2692 2693
    }

2694
    err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2695 2696 2697
                           &newdirfidp->path, new_name);
    if (err < 0) {
        goto out;
2698
    }
2699
    if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2700
        /* Only for path based fid  we need to do the below fixup */
2701
        v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2702 2703
                           &newdirfidp->path, new_name);
    }
2704 2705
out:
    if (olddirfidp) {
2706
        put_fid(pdu, olddirfidp);
2707 2708
    }
    if (newdirfidp) {
2709
        put_fid(pdu, newdirfidp);
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    }
    return err;
}

static void v9fs_renameat(void *opaque)
{
    ssize_t err = 0;
    size_t offset = 7;
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
    int32_t olddirfid, newdirfid;
    V9fsString old_name, new_name;

2723 2724 2725 2726 2727 2728 2729
    v9fs_string_init(&old_name);
    v9fs_string_init(&new_name);
    err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
                        &old_name, &newdirfid, &new_name);
    if (err < 0) {
        goto out_err;
    }
2730

G
Greg Kurz 已提交
2731 2732 2733 2734 2735
    if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
        err = -ENOENT;
        goto out_err;
    }

G
Greg Kurz 已提交
2736 2737 2738 2739 2740 2741
    if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
        !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
        err = -EISDIR;
        goto out_err;
    }

2742
    v9fs_path_write_lock(s);
2743 2744
    err = v9fs_complete_renameat(pdu, olddirfid,
                                 &old_name, newdirfid, &new_name);
2745
    v9fs_path_unlock(s);
2746 2747 2748
    if (!err) {
        err = offset;
    }
2749 2750

out_err:
2751
    pdu_complete(pdu, err);
2752 2753 2754 2755
    v9fs_string_free(&old_name);
    v9fs_string_free(&new_name);
}

2756
static void v9fs_wstat(void *opaque)
2757
{
2758 2759 2760 2761 2762 2763 2764 2765
    int32_t fid;
    int err = 0;
    int16_t unused;
    V9fsStat v9stat;
    size_t offset = 7;
    struct stat stbuf;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
2766

2767 2768 2769 2770 2771
    v9fs_stat_init(&v9stat);
    err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
    if (err < 0) {
        goto out_nofid;
    }
2772 2773
    trace_v9fs_wstat(pdu->tag, pdu->id, fid,
                     v9stat.mode, v9stat.atime, v9stat.mtime);
2774

2775
    fidp = get_fid(pdu, fid);
2776 2777
    if (fidp == NULL) {
        err = -EINVAL;
2778
        goto out_nofid;
2779
    }
2780 2781
    /* do we need to sync the file? */
    if (donttouch_stat(&v9stat)) {
2782
        err = v9fs_co_fsync(pdu, fidp, 0);
2783 2784
        goto out;
    }
2785 2786
    if (v9stat.mode != -1) {
        uint32_t v9_mode;
2787
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
        if (err < 0) {
            goto out;
        }
        v9_mode = stat_to_v9mode(&stbuf);
        if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
            (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
            /* Attempting to change the type */
            err = -EIO;
            goto out;
        }
2798
        err = v9fs_co_chmod(pdu, &fidp->path,
2799 2800 2801 2802 2803 2804 2805
                            v9mode_to_mode(v9stat.mode,
                                           &v9stat.extension));
        if (err < 0) {
            goto out;
        }
    }
    if (v9stat.mtime != -1 || v9stat.atime != -1) {
2806
        struct timespec times[2];
2807 2808
        if (v9stat.atime != -1) {
            times[0].tv_sec = v9stat.atime;
2809 2810 2811 2812
            times[0].tv_nsec = 0;
        } else {
            times[0].tv_nsec = UTIME_OMIT;
        }
2813 2814
        if (v9stat.mtime != -1) {
            times[1].tv_sec = v9stat.mtime;
2815 2816 2817 2818
            times[1].tv_nsec = 0;
        } else {
            times[1].tv_nsec = UTIME_OMIT;
        }
2819
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
2820 2821
        if (err < 0) {
            goto out;
2822 2823
        }
    }
2824
    if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2825
        err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2826
        if (err < 0) {
2827
            goto out;
2828
        }
2829
    }
2830
    if (v9stat.name.size != 0) {
2831
        err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2832 2833 2834
        if (err < 0) {
            goto out;
        }
2835
    }
2836
    if (v9stat.length != -1) {
2837
        err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2838 2839 2840
        if (err < 0) {
            goto out;
        }
2841
    }
2842
    err = offset;
2843
out:
2844
    put_fid(pdu, fidp);
2845
out_nofid:
2846
    v9fs_stat_free(&v9stat);
2847
    pdu_complete(pdu, err);
2848 2849
}

2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
{
    uint32_t f_type;
    uint32_t f_bsize;
    uint64_t f_blocks;
    uint64_t f_bfree;
    uint64_t f_bavail;
    uint64_t f_files;
    uint64_t f_ffree;
    uint64_t fsid_val;
    uint32_t f_namelen;
    size_t offset = 7;
2862 2863 2864 2865 2866 2867
    int32_t bsize_factor;

    /*
     * compute bsize factor based on host file system block size
     * and client msize
     */
2868
    bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2869 2870 2871
    if (!bsize_factor) {
        bsize_factor = 1;
    }
2872 2873 2874
    f_type  = stbuf->f_type;
    f_bsize = stbuf->f_bsize;
    f_bsize *= bsize_factor;
2875 2876 2877 2878 2879
    /*
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
     * adjust(divide) the number of blocks, free blocks and available
     * blocks by bsize factor
     */
2880 2881 2882 2883 2884 2885 2886 2887
    f_blocks = stbuf->f_blocks/bsize_factor;
    f_bfree  = stbuf->f_bfree/bsize_factor;
    f_bavail = stbuf->f_bavail/bsize_factor;
    f_files  = stbuf->f_files;
    f_ffree  = stbuf->f_ffree;
    fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
               (unsigned long long)stbuf->f_fsid.__val[1] << 32;
    f_namelen = stbuf->f_namelen;
2888

2889 2890 2891 2892
    return pdu_marshal(pdu, offset, "ddqqqqqqd",
                       f_type, f_bsize, f_blocks, f_bfree,
                       f_bavail, f_files, f_ffree,
                       fsid_val, f_namelen);
2893 2894
}

2895
static void v9fs_statfs(void *opaque)
2896
{
2897 2898 2899 2900 2901
    int32_t fid;
    ssize_t retval = 0;
    size_t offset = 7;
    V9fsFidState *fidp;
    struct statfs stbuf;
2902 2903
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
2904

2905 2906 2907 2908
    retval = pdu_unmarshal(pdu, offset, "d", &fid);
    if (retval < 0) {
        goto out_nofid;
    }
2909
    fidp = get_fid(pdu, fid);
2910 2911
    if (fidp == NULL) {
        retval = -ENOENT;
2912
        goto out_nofid;
2913
    }
2914
    retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2915 2916 2917
    if (retval < 0) {
        goto out;
    }
2918 2919 2920 2921 2922
    retval = v9fs_fill_statfs(s, pdu, &stbuf);
    if (retval < 0) {
        goto out;
    }
    retval += offset;
2923
out:
2924
    put_fid(pdu, fidp);
2925
out_nofid:
2926
    pdu_complete(pdu, retval);
2927 2928
}

2929
static void v9fs_mknod(void *opaque)
2930
{
2931 2932 2933

    int mode;
    gid_t gid;
2934
    int32_t fid;
2935
    V9fsQID qid;
2936 2937
    int err = 0;
    int major, minor;
2938 2939 2940 2941 2942
    size_t offset = 7;
    V9fsString name;
    struct stat stbuf;
    V9fsFidState *fidp;
    V9fsPDU *pdu = opaque;
2943

2944 2945 2946 2947 2948 2949
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
                        &major, &minor, &gid);
    if (err < 0) {
        goto out_nofid;
    }
2950
    trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2951

G
Greg Kurz 已提交
2952 2953 2954 2955 2956
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
2957 2958 2959 2960 2961
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

2962
    fidp = get_fid(pdu, fid);
2963 2964
    if (fidp == NULL) {
        err = -ENOENT;
2965
        goto out_nofid;
2966
    }
2967
    err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2968
                        makedev(major, minor), mode, &stbuf);
2969 2970 2971 2972
    if (err < 0) {
        goto out;
    }
    stat_to_qid(&stbuf, &qid);
2973 2974 2975 2976 2977
    err = pdu_marshal(pdu, offset, "Q", &qid);
    if (err < 0) {
        goto out;
    }
    err += offset;
2978 2979
    trace_v9fs_mknod_return(pdu->tag, pdu->id,
                            qid.type, qid.version, qid.path);
2980
out:
2981
    put_fid(pdu, fidp);
2982
out_nofid:
2983
    pdu_complete(pdu, err);
2984
    v9fs_string_free(&name);
2985 2986
}

M
M. Mohan Kumar 已提交
2987 2988 2989 2990 2991 2992 2993 2994
/*
 * Implement posix byte range locking code
 * Server side handling of locking code is very simple, because 9p server in
 * QEMU can handle only one client. And most of the lock handling
 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
 * do any thing in * qemu 9p server side lock code path.
 * So when a TLOCK request comes, always return success
 */
2995
static void v9fs_lock(void *opaque)
M
M. Mohan Kumar 已提交
2996
{
2997
    int8_t status;
2998
    V9fsFlock flock;
2999 3000 3001 3002
    size_t offset = 7;
    struct stat stbuf;
    V9fsFidState *fidp;
    int32_t fid, err = 0;
3003
    V9fsPDU *pdu = opaque;
M
M. Mohan Kumar 已提交
3004

3005 3006 3007 3008 3009 3010 3011 3012
    status = P9_LOCK_ERROR;
    v9fs_string_init(&flock.client_id);
    err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
                        &flock.flags, &flock.start, &flock.length,
                        &flock.proc_id, &flock.client_id);
    if (err < 0) {
        goto out_nofid;
    }
3013
    trace_v9fs_lock(pdu->tag, pdu->id, fid,
3014
                    flock.type, flock.start, flock.length);
3015

M
M. Mohan Kumar 已提交
3016 3017

    /* We support only block flag now (that too ignored currently) */
3018
    if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
M
M. Mohan Kumar 已提交
3019
        err = -EINVAL;
3020
        goto out_nofid;
M
M. Mohan Kumar 已提交
3021
    }
3022
    fidp = get_fid(pdu, fid);
3023
    if (fidp == NULL) {
M
M. Mohan Kumar 已提交
3024
        err = -ENOENT;
3025
        goto out_nofid;
M
M. Mohan Kumar 已提交
3026
    }
3027
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
M
M. Mohan Kumar 已提交
3028 3029 3030
    if (err < 0) {
        goto out;
    }
3031
    status = P9_LOCK_SUCCESS;
M
M. Mohan Kumar 已提交
3032
out:
3033
    put_fid(pdu, fidp);
3034
out_nofid:
3035 3036 3037 3038
    err = pdu_marshal(pdu, offset, "b", status);
    if (err > 0) {
        err += offset;
    }
3039
    trace_v9fs_lock_return(pdu->tag, pdu->id, status);
3040
    pdu_complete(pdu, err);
3041
    v9fs_string_free(&flock.client_id);
M
M. Mohan Kumar 已提交
3042 3043
}

M
M. Mohan Kumar 已提交
3044 3045 3046 3047
/*
 * When a TGETLOCK request comes, always return success because all lock
 * handling is done by client's VFS layer.
 */
3048
static void v9fs_getlock(void *opaque)
M
M. Mohan Kumar 已提交
3049
{
3050 3051 3052
    size_t offset = 7;
    struct stat stbuf;
    V9fsFidState *fidp;
3053
    V9fsGetlock glock;
3054
    int32_t fid, err = 0;
3055
    V9fsPDU *pdu = opaque;
M
M. Mohan Kumar 已提交
3056

3057 3058 3059 3060 3061 3062 3063
    v9fs_string_init(&glock.client_id);
    err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
                        &glock.start, &glock.length, &glock.proc_id,
                        &glock.client_id);
    if (err < 0) {
        goto out_nofid;
    }
3064
    trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3065
                       glock.type, glock.start, glock.length);
3066

3067
    fidp = get_fid(pdu, fid);
3068
    if (fidp == NULL) {
M
M. Mohan Kumar 已提交
3069
        err = -ENOENT;
3070
        goto out_nofid;
M
M. Mohan Kumar 已提交
3071
    }
3072
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
M
M. Mohan Kumar 已提交
3073 3074 3075
    if (err < 0) {
        goto out;
    }
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
    glock.type = P9_LOCK_TYPE_UNLCK;
    err = pdu_marshal(pdu, offset, "bqqds", glock.type,
                          glock.start, glock.length, glock.proc_id,
                          &glock.client_id);
    if (err < 0) {
        goto out;
    }
    err += offset;
    trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
                              glock.length, glock.proc_id);
M
M. Mohan Kumar 已提交
3086
out:
3087
    put_fid(pdu, fidp);
3088
out_nofid:
3089
    pdu_complete(pdu, err);
3090
    v9fs_string_free(&glock.client_id);
M
M. Mohan Kumar 已提交
3091 3092
}

3093
static void v9fs_mkdir(void *opaque)
3094
{
3095
    V9fsPDU *pdu = opaque;
3096
    size_t offset = 7;
3097
    int32_t fid;
3098 3099
    struct stat stbuf;
    V9fsQID qid;
3100
    V9fsString name;
3101 3102 3103
    V9fsFidState *fidp;
    gid_t gid;
    int mode;
3104
    int err = 0;
3105

3106 3107 3108 3109 3110
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
    if (err < 0) {
        goto out_nofid;
    }
3111 3112
    trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);

G
Greg Kurz 已提交
3113 3114 3115 3116 3117
    if (name_is_illegal(name.data)) {
        err = -ENOENT;
        goto out_nofid;
    }

G
Greg Kurz 已提交
3118 3119 3120 3121 3122
    if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
        err = -EEXIST;
        goto out_nofid;
    }

3123
    fidp = get_fid(pdu, fid);
3124 3125
    if (fidp == NULL) {
        err = -ENOENT;
3126
        goto out_nofid;
3127
    }
3128
    err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3129 3130 3131 3132
    if (err < 0) {
        goto out;
    }
    stat_to_qid(&stbuf, &qid);
3133 3134 3135 3136 3137
    err = pdu_marshal(pdu, offset, "Q", &qid);
    if (err < 0) {
        goto out;
    }
    err += offset;
3138 3139
    trace_v9fs_mkdir_return(pdu->tag, pdu->id,
                            qid.type, qid.version, qid.path, err);
3140
out:
3141
    put_fid(pdu, fidp);
3142
out_nofid:
3143
    pdu_complete(pdu, err);
3144
    v9fs_string_free(&name);
3145 3146
}

3147
static void v9fs_xattrwalk(void *opaque)
3148
{
3149 3150
    int64_t size;
    V9fsString name;
3151
    ssize_t err = 0;
3152
    size_t offset = 7;
3153
    int32_t fid, newfid;
3154
    V9fsFidState *file_fidp;
3155
    V9fsFidState *xattr_fidp = NULL;
3156 3157
    V9fsPDU *pdu = opaque;
    V9fsState *s = pdu->s;
3158

3159 3160 3161 3162 3163
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
    if (err < 0) {
        goto out_nofid;
    }
3164 3165
    trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);

3166
    file_fidp = get_fid(pdu, fid);
3167
    if (file_fidp == NULL) {
3168
        err = -ENOENT;
3169
        goto out_nofid;
3170
    }
3171 3172
    xattr_fidp = alloc_fid(s, newfid);
    if (xattr_fidp == NULL) {
3173 3174 3175
        err = -EINVAL;
        goto out;
    }
3176
    v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3177
    if (name.data == NULL) {
3178 3179 3180
        /*
         * listxattr request. Get the size first
         */
3181
        size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3182 3183
        if (size < 0) {
            err = size;
3184
            clunk_fid(s, xattr_fidp->fid);
3185
            goto out;
3186
        }
3187 3188 3189 3190 3191 3192 3193
        /*
         * Read the xattr value
         */
        xattr_fidp->fs.xattr.len = size;
        xattr_fidp->fid_type = P9_FID_XATTR;
        xattr_fidp->fs.xattr.copied_len = -1;
        if (size) {
3194
            xattr_fidp->fs.xattr.value = g_malloc(size);
3195
            err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3196 3197 3198
                                     xattr_fidp->fs.xattr.value,
                                     xattr_fidp->fs.xattr.len);
            if (err < 0) {
3199
                clunk_fid(s, xattr_fidp->fid);
3200 3201 3202
                goto out;
            }
        }
3203 3204 3205 3206 3207
        err = pdu_marshal(pdu, offset, "q", size);
        if (err < 0) {
            goto out;
        }
        err += offset;
3208 3209 3210 3211 3212
    } else {
        /*
         * specific xattr fid. We check for xattr
         * presence also collect the xattr size
         */
3213
        size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3214 3215 3216
                                 &name, NULL, 0);
        if (size < 0) {
            err = size;
3217
            clunk_fid(s, xattr_fidp->fid);
3218
            goto out;
3219
        }
3220 3221 3222 3223 3224 3225 3226
        /*
         * Read the xattr value
         */
        xattr_fidp->fs.xattr.len = size;
        xattr_fidp->fid_type = P9_FID_XATTR;
        xattr_fidp->fs.xattr.copied_len = -1;
        if (size) {
3227
            xattr_fidp->fs.xattr.value = g_malloc(size);
3228
            err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3229 3230 3231
                                    &name, xattr_fidp->fs.xattr.value,
                                    xattr_fidp->fs.xattr.len);
            if (err < 0) {
3232
                clunk_fid(s, xattr_fidp->fid);
3233 3234 3235
                goto out;
            }
        }
3236 3237 3238 3239 3240
        err = pdu_marshal(pdu, offset, "q", size);
        if (err < 0) {
            goto out;
        }
        err += offset;
3241
    }
3242
    trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3243
out:
3244
    put_fid(pdu, file_fidp);
3245
    if (xattr_fidp) {
3246
        put_fid(pdu, xattr_fidp);
3247 3248
    }
out_nofid:
3249
    pdu_complete(pdu, err);
3250
    v9fs_string_free(&name);
3251 3252
}

3253
static void v9fs_xattrcreate(void *opaque)
3254 3255 3256
{
    int flags;
    int32_t fid;
3257
    int64_t size;
3258
    ssize_t err = 0;
3259 3260 3261 3262 3263
    V9fsString name;
    size_t offset = 7;
    V9fsFidState *file_fidp;
    V9fsFidState *xattr_fidp;
    V9fsPDU *pdu = opaque;
3264

3265 3266 3267 3268 3269
    v9fs_string_init(&name);
    err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
    if (err < 0) {
        goto out_nofid;
    }
3270
    trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3271

3272
    file_fidp = get_fid(pdu, fid);
3273
    if (file_fidp == NULL) {
3274
        err = -EINVAL;
3275
        goto out_nofid;
3276 3277
    }
    /* Make the file fid point to xattr */
3278 3279 3280 3281 3282 3283 3284
    xattr_fidp = file_fidp;
    xattr_fidp->fid_type = P9_FID_XATTR;
    xattr_fidp->fs.xattr.copied_len = 0;
    xattr_fidp->fs.xattr.len = size;
    xattr_fidp->fs.xattr.flags = flags;
    v9fs_string_init(&xattr_fidp->fs.xattr.name);
    v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3285
    xattr_fidp->fs.xattr.value = g_malloc(size);
3286
    err = offset;
3287
    put_fid(pdu, file_fidp);
3288
out_nofid:
3289
    pdu_complete(pdu, err);
3290
    v9fs_string_free(&name);
3291
}
3292

3293
static void v9fs_readlink(void *opaque)
3294
{
3295
    V9fsPDU *pdu = opaque;
3296 3297
    size_t offset = 7;
    V9fsString target;
3298 3299 3300 3301
    int32_t fid;
    int err = 0;
    V9fsFidState *fidp;

3302 3303 3304 3305
    err = pdu_unmarshal(pdu, offset, "d", &fid);
    if (err < 0) {
        goto out_nofid;
    }
3306
    trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3307
    fidp = get_fid(pdu, fid);
3308 3309
    if (fidp == NULL) {
        err = -ENOENT;
3310
        goto out_nofid;
3311 3312
    }

3313
    v9fs_string_init(&target);
3314
    err = v9fs_co_readlink(pdu, &fidp->path, &target);
3315 3316 3317
    if (err < 0) {
        goto out;
    }
3318 3319 3320 3321 3322 3323
    err = pdu_marshal(pdu, offset, "s", &target);
    if (err < 0) {
        v9fs_string_free(&target);
        goto out;
    }
    err += offset;
3324
    trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3325
    v9fs_string_free(&target);
3326
out:
3327
    put_fid(pdu, fidp);
3328
out_nofid:
3329
    pdu_complete(pdu, err);
3330 3331
}

3332
static CoroutineEntry *pdu_co_handlers[] = {
3333
    [P9_TREADDIR] = v9fs_readdir,
3334
    [P9_TSTATFS] = v9fs_statfs,
3335
    [P9_TGETATTR] = v9fs_getattr,
3336
    [P9_TSETATTR] = v9fs_setattr,
3337
    [P9_TXATTRWALK] = v9fs_xattrwalk,
3338
    [P9_TXATTRCREATE] = v9fs_xattrcreate,
3339
    [P9_TMKNOD] = v9fs_mknod,
3340
    [P9_TRENAME] = v9fs_rename,
M
M. Mohan Kumar 已提交
3341
    [P9_TLOCK] = v9fs_lock,
M
M. Mohan Kumar 已提交
3342
    [P9_TGETLOCK] = v9fs_getlock,
3343
    [P9_TRENAMEAT] = v9fs_renameat,
3344
    [P9_TREADLINK] = v9fs_readlink,
3345
    [P9_TUNLINKAT] = v9fs_unlinkat,
3346
    [P9_TMKDIR] = v9fs_mkdir,
3347
    [P9_TVERSION] = v9fs_version,
3348
    [P9_TLOPEN] = v9fs_open,
3349 3350 3351 3352
    [P9_TATTACH] = v9fs_attach,
    [P9_TSTAT] = v9fs_stat,
    [P9_TWALK] = v9fs_walk,
    [P9_TCLUNK] = v9fs_clunk,
3353
    [P9_TFSYNC] = v9fs_fsync,
3354 3355 3356 3357 3358 3359
    [P9_TOPEN] = v9fs_open,
    [P9_TREAD] = v9fs_read,
#if 0
    [P9_TAUTH] = v9fs_auth,
#endif
    [P9_TFLUSH] = v9fs_flush,
3360
    [P9_TLINK] = v9fs_link,
3361
    [P9_TSYMLINK] = v9fs_symlink,
3362
    [P9_TCREATE] = v9fs_create,
3363
    [P9_TLCREATE] = v9fs_lcreate,
3364 3365 3366 3367 3368
    [P9_TWRITE] = v9fs_write,
    [P9_TWSTAT] = v9fs_wstat,
    [P9_TREMOVE] = v9fs_remove,
};

3369
static void v9fs_op_not_supp(void *opaque)
3370
{
3371
    V9fsPDU *pdu = opaque;
3372
    pdu_complete(pdu, -EOPNOTSUPP);
3373 3374
}

3375 3376 3377
static void v9fs_fs_ro(void *opaque)
{
    V9fsPDU *pdu = opaque;
3378
    pdu_complete(pdu, -EROFS);
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
}

static inline bool is_read_only_op(V9fsPDU *pdu)
{
    switch (pdu->id) {
    case P9_TREADDIR:
    case P9_TSTATFS:
    case P9_TGETATTR:
    case P9_TXATTRWALK:
    case P9_TLOCK:
    case P9_TGETLOCK:
    case P9_TREADLINK:
    case P9_TVERSION:
    case P9_TLOPEN:
    case P9_TATTACH:
    case P9_TSTAT:
    case P9_TWALK:
    case P9_TCLUNK:
    case P9_TFSYNC:
    case P9_TOPEN:
    case P9_TREAD:
    case P9_TAUTH:
    case P9_TFLUSH:
        return 1;
    default:
        return 0;
    }
}

W
Wei Liu 已提交
3408
void pdu_submit(V9fsPDU *pdu)
3409
{
3410 3411
    Coroutine *co;
    CoroutineEntry *handler;
3412
    V9fsState *s = pdu->s;
3413

3414 3415
    if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
        (pdu_co_handlers[pdu->id] == NULL)) {
3416 3417
        handler = v9fs_op_not_supp;
    } else {
3418
        handler = pdu_co_handlers[pdu->id];
3419
    }
3420 3421 3422 3423

    if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
        handler = v9fs_fs_ro;
    }
3424 3425
    co = qemu_coroutine_create(handler, pdu);
    qemu_coroutine_enter(co);
3426 3427
}

3428 3429 3430
/* Returns 0 on success, 1 on failure. */
int v9fs_device_realize_common(V9fsState *s, Error **errp)
{
W
Wei Liu 已提交
3431
    V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441
    int i, len;
    struct stat stat;
    FsDriverEntry *fse;
    V9fsPath path;
    int rc = 1;

    /* initialize pdu allocator */
    QLIST_INIT(&s->free_list);
    QLIST_INIT(&s->active_list);
    for (i = 0; i < (MAX_REQ - 1); i++) {
W
Wei Liu 已提交
3442 3443 3444
        QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
        v->pdus[i].s = s;
        v->pdus[i].idx = i;
3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
    }

    v9fs_path_init(&path);

    fse = get_fsdev_fsentry(s->fsconf.fsdev_id);

    if (!fse) {
        /* We don't have a fsdev identified by fsdev_id */
        error_setg(errp, "9pfs device couldn't find fsdev with the "
                   "id = %s",
                   s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
        goto out;
    }

    if (!s->fsconf.tag) {
        /* we haven't specified a mount_tag */
        error_setg(errp, "fsdev with id %s needs mount_tag arguments",
                   s->fsconf.fsdev_id);
        goto out;
    }

    s->ctx.export_flags = fse->export_flags;
    s->ctx.fs_root = g_strdup(fse->path);
    s->ctx.exops.get_st_gen = NULL;
    len = strlen(s->fsconf.tag);
    if (len > MAX_TAG_LEN - 1) {
        error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
                   "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
        goto out;
    }

    s->tag = g_strdup(s->fsconf.tag);
    s->ctx.uid = -1;

    s->ops = fse->ops;

    s->fid_list = NULL;
    qemu_co_rwlock_init(&s->rename_lock);

    if (s->ops->init(&s->ctx) < 0) {
        error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
                   " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
        goto out;
    }

    /*
     * Check details of export path, We need to use fs driver
     * call back to do that. Since we are in the init path, we don't
     * use co-routines here.
     */
    if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
        error_setg(errp,
                   "error in converting name to path %s", strerror(errno));
        goto out;
    }
    if (s->ops->lstat(&s->ctx, &path, &stat)) {
        error_setg(errp, "share path %s does not exist", fse->path);
        goto out;
    } else if (!S_ISDIR(stat.st_mode)) {
        error_setg(errp, "share path %s is not a directory", fse->path);
        goto out;
    }
    v9fs_path_free(&path);

    rc = 0;
out:
    if (rc) {
        g_free(s->ctx.fs_root);
        g_free(s->tag);
        v9fs_path_free(&path);
    }
    return rc;
}

void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
{
    g_free(s->ctx.fs_root);
    g_free(s->tag);
}

3525
static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3526 3527 3528
{
    struct rlimit rlim;
    if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3529
        error_report("Failed to get the resource limit");
3530 3531 3532 3533 3534
        exit(1);
    }
    open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
    open_fd_rc = rlim.rlim_cur/2;
}