qemu/hw/9pfs/9p-proxy.c
<<
>>
Prefs
   1/*
   2 * 9p Proxy callback
   3 *
   4 * Copyright IBM, Corp. 2011
   5 *
   6 * Authors:
   7 * M. Mohan Kumar <mohan@in.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2.  See
  10 * the COPYING file in the top-level directory.
  11 */
  12#include "qemu/osdep.h"
  13#include <sys/socket.h>
  14#include <sys/un.h>
  15#include "9p.h"
  16#include "qemu/cutils.h"
  17#include "qemu/error-report.h"
  18#include "fsdev/qemu-fsdev.h"
  19#include "9p-proxy.h"
  20
  21typedef struct V9fsProxy {
  22    int sockfd;
  23    QemuMutex mutex;
  24    struct iovec in_iovec;
  25    struct iovec out_iovec;
  26} V9fsProxy;
  27
  28/*
  29 * Return received file descriptor on success in *status.
  30 * errno is also returned on *status (which will be < 0)
  31 * return < 0 on transport error.
  32 */
  33static int v9fs_receivefd(int sockfd, int *status)
  34{
  35    struct iovec iov;
  36    struct msghdr msg;
  37    struct cmsghdr *cmsg;
  38    int retval, data, fd;
  39    union MsgControl msg_control;
  40
  41    iov.iov_base = &data;
  42    iov.iov_len = sizeof(data);
  43
  44    memset(&msg, 0, sizeof(msg));
  45    msg.msg_iov = &iov;
  46    msg.msg_iovlen = 1;
  47    msg.msg_control = &msg_control;
  48    msg.msg_controllen = sizeof(msg_control);
  49
  50    do {
  51        retval = recvmsg(sockfd, &msg, 0);
  52    } while (retval < 0 && errno == EINTR);
  53    if (retval <= 0) {
  54        return retval;
  55    }
  56    /*
  57     * data is set to V9FS_FD_VALID, if ancillary data is sent.  If this
  58     * request doesn't need ancillary data (fd) or an error occurred,
  59     * data is set to negative errno value.
  60     */
  61    if (data != V9FS_FD_VALID) {
  62        *status = data;
  63        return 0;
  64    }
  65    /*
  66     * File descriptor (fd) is sent in the ancillary data. Check if we
  67     * indeed received it. One of the reasons to fail to receive it is if
  68     * we exceeded the maximum number of file descriptors!
  69     */
  70    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  71        if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
  72            cmsg->cmsg_level != SOL_SOCKET ||
  73            cmsg->cmsg_type != SCM_RIGHTS) {
  74            continue;
  75        }
  76        fd = *((int *)CMSG_DATA(cmsg));
  77        *status = fd;
  78        return 0;
  79    }
  80    *status = -ENFILE;  /* Ancillary data sent but not received */
  81    return 0;
  82}
  83
  84static ssize_t socket_read(int sockfd, void *buff, size_t size)
  85{
  86    ssize_t retval, total = 0;
  87
  88    while (size) {
  89        retval = read(sockfd, buff, size);
  90        if (retval == 0) {
  91            return -EIO;
  92        }
  93        if (retval < 0) {
  94            if (errno == EINTR) {
  95                continue;
  96            }
  97            return -errno;
  98        }
  99        size -= retval;
 100        buff += retval;
 101        total += retval;
 102    }
 103    return total;
 104}
 105
 106/* Converts proxy_statfs to VFS statfs structure */
 107static void prstatfs_to_statfs(struct statfs *stfs, ProxyStatFS *prstfs)
 108{
 109    memset(stfs, 0, sizeof(*stfs));
 110    stfs->f_type = prstfs->f_type;
 111    stfs->f_bsize = prstfs->f_bsize;
 112    stfs->f_blocks = prstfs->f_blocks;
 113    stfs->f_bfree = prstfs->f_bfree;
 114    stfs->f_bavail = prstfs->f_bavail;
 115    stfs->f_files = prstfs->f_files;
 116    stfs->f_ffree = prstfs->f_ffree;
 117    stfs->f_fsid.__val[0] = prstfs->f_fsid[0] & 0xFFFFFFFFU;
 118    stfs->f_fsid.__val[1] = prstfs->f_fsid[1] >> 32 & 0xFFFFFFFFU;
 119    stfs->f_namelen = prstfs->f_namelen;
 120    stfs->f_frsize = prstfs->f_frsize;
 121}
 122
 123/* Converts proxy_stat structure to VFS stat structure */
 124static void prstat_to_stat(struct stat *stbuf, ProxyStat *prstat)
 125{
 126   memset(stbuf, 0, sizeof(*stbuf));
 127   stbuf->st_dev = prstat->st_dev;
 128   stbuf->st_ino = prstat->st_ino;
 129   stbuf->st_nlink = prstat->st_nlink;
 130   stbuf->st_mode = prstat->st_mode;
 131   stbuf->st_uid = prstat->st_uid;
 132   stbuf->st_gid = prstat->st_gid;
 133   stbuf->st_rdev = prstat->st_rdev;
 134   stbuf->st_size = prstat->st_size;
 135   stbuf->st_blksize = prstat->st_blksize;
 136   stbuf->st_blocks = prstat->st_blocks;
 137   stbuf->st_atim.tv_sec = prstat->st_atim_sec;
 138   stbuf->st_atim.tv_nsec = prstat->st_atim_nsec;
 139   stbuf->st_mtime = prstat->st_mtim_sec;
 140   stbuf->st_mtim.tv_nsec = prstat->st_mtim_nsec;
 141   stbuf->st_ctime = prstat->st_ctim_sec;
 142   stbuf->st_ctim.tv_nsec = prstat->st_ctim_nsec;
 143}
 144
 145/*
 146 * Response contains two parts
 147 * {header, data}
 148 * header.type == T_ERROR, data -> -errno
 149 * header.type == T_SUCCESS, data -> response
 150 * size of errno/response is given by header.size
 151 * returns < 0, on transport error. response is
 152 * valid only if status >= 0.
 153 */
 154static int v9fs_receive_response(V9fsProxy *proxy, int type,
 155                                 int *status, void *response)
 156{
 157    int retval;
 158    ProxyHeader header;
 159    struct iovec *reply = &proxy->in_iovec;
 160
 161    *status = 0;
 162    reply->iov_len = 0;
 163    retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
 164    if (retval < 0) {
 165        return retval;
 166    }
 167    reply->iov_len = PROXY_HDR_SZ;
 168    proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
 169    /*
 170     * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
 171     * return -ENOBUFS
 172     */
 173    if (header.size > PROXY_MAX_IO_SZ) {
 174        int count;
 175        while (header.size > 0) {
 176            count = MIN(PROXY_MAX_IO_SZ, header.size);
 177            count = socket_read(proxy->sockfd, reply->iov_base, count);
 178            if (count < 0) {
 179                return count;
 180            }
 181            header.size -= count;
 182        }
 183        *status = -ENOBUFS;
 184        return 0;
 185    }
 186
 187    retval = socket_read(proxy->sockfd,
 188                         reply->iov_base + PROXY_HDR_SZ, header.size);
 189    if (retval < 0) {
 190        return retval;
 191    }
 192    reply->iov_len += header.size;
 193    /* there was an error during processing request */
 194    if (header.type == T_ERROR) {
 195        int ret;
 196        ret = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
 197        if (ret < 0) {
 198            *status = ret;
 199        }
 200        return 0;
 201    }
 202
 203    switch (type) {
 204    case T_LSTAT: {
 205        ProxyStat prstat;
 206        retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
 207                                 "qqqdddqqqqqqqqqq", &prstat.st_dev,
 208                                 &prstat.st_ino, &prstat.st_nlink,
 209                                 &prstat.st_mode, &prstat.st_uid,
 210                                 &prstat.st_gid, &prstat.st_rdev,
 211                                 &prstat.st_size, &prstat.st_blksize,
 212                                 &prstat.st_blocks,
 213                                 &prstat.st_atim_sec, &prstat.st_atim_nsec,
 214                                 &prstat.st_mtim_sec, &prstat.st_mtim_nsec,
 215                                 &prstat.st_ctim_sec, &prstat.st_ctim_nsec);
 216        prstat_to_stat(response, &prstat);
 217        break;
 218    }
 219    case T_STATFS: {
 220        ProxyStatFS prstfs;
 221        retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
 222                                 "qqqqqqqqqqq", &prstfs.f_type,
 223                                 &prstfs.f_bsize, &prstfs.f_blocks,
 224                                 &prstfs.f_bfree, &prstfs.f_bavail,
 225                                 &prstfs.f_files, &prstfs.f_ffree,
 226                                 &prstfs.f_fsid[0], &prstfs.f_fsid[1],
 227                                 &prstfs.f_namelen, &prstfs.f_frsize);
 228        prstatfs_to_statfs(response, &prstfs);
 229        break;
 230    }
 231    case T_READLINK: {
 232        V9fsString target;
 233        v9fs_string_init(&target);
 234        retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &target);
 235        strcpy(response, target.data);
 236        v9fs_string_free(&target);
 237        break;
 238    }
 239    case T_LGETXATTR:
 240    case T_LLISTXATTR: {
 241        V9fsString xattr;
 242        v9fs_string_init(&xattr);
 243        retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &xattr);
 244        memcpy(response, xattr.data, xattr.size);
 245        v9fs_string_free(&xattr);
 246        break;
 247    }
 248    case T_GETVERSION:
 249        proxy_unmarshal(reply, PROXY_HDR_SZ, "q", response);
 250        break;
 251    default:
 252        return -1;
 253    }
 254    if (retval < 0) {
 255        *status  = retval;
 256    }
 257    return 0;
 258}
 259
 260/*
 261 * return < 0 on transport error.
 262 * *status is valid only if return >= 0
 263 */
 264static int v9fs_receive_status(V9fsProxy *proxy,
 265                               struct iovec *reply, int *status)
 266{
 267    int retval;
 268    ProxyHeader header;
 269
 270    *status = 0;
 271    reply->iov_len = 0;
 272    retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
 273    if (retval < 0) {
 274        return retval;
 275    }
 276    reply->iov_len = PROXY_HDR_SZ;
 277    proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
 278    if (header.size != sizeof(int)) {
 279        *status = -ENOBUFS;
 280        return 0;
 281    }
 282    retval = socket_read(proxy->sockfd,
 283                         reply->iov_base + PROXY_HDR_SZ, header.size);
 284    if (retval < 0) {
 285        return retval;
 286    }
 287    reply->iov_len += header.size;
 288    proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
 289    return 0;
 290}
 291
 292/*
 293 * Proxy->header and proxy->request written to socket by QEMU process.
 294 * This request read by proxy helper process
 295 * returns 0 on success and -errno on error
 296 */
 297static int v9fs_request(V9fsProxy *proxy, int type,
 298                        void *response, const char *fmt, ...)
 299{
 300    dev_t rdev;
 301    va_list ap;
 302    int size = 0;
 303    int retval = 0;
 304    uint64_t offset;
 305    ProxyHeader header = { 0, 0};
 306    struct timespec spec[2];
 307    int flags, mode, uid, gid;
 308    V9fsString *name, *value;
 309    V9fsString *path, *oldpath;
 310    struct iovec *iovec = NULL, *reply = NULL;
 311
 312    qemu_mutex_lock(&proxy->mutex);
 313
 314    if (proxy->sockfd == -1) {
 315        retval = -EIO;
 316        goto err_out;
 317    }
 318    iovec = &proxy->out_iovec;
 319    reply = &proxy->in_iovec;
 320    va_start(ap, fmt);
 321    switch (type) {
 322    case T_OPEN:
 323        path = va_arg(ap, V9fsString *);
 324        flags = va_arg(ap, int);
 325        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags);
 326        if (retval > 0) {
 327            header.size = retval;
 328            header.type = T_OPEN;
 329        }
 330        break;
 331    case T_CREATE:
 332        path = va_arg(ap, V9fsString *);
 333        flags = va_arg(ap, int);
 334        mode = va_arg(ap, int);
 335        uid = va_arg(ap, int);
 336        gid = va_arg(ap, int);
 337        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdddd", path,
 338                                    flags, mode, uid, gid);
 339        if (retval > 0) {
 340            header.size = retval;
 341            header.type = T_CREATE;
 342        }
 343        break;
 344    case T_MKNOD:
 345        path = va_arg(ap, V9fsString *);
 346        mode = va_arg(ap, int);
 347        rdev = va_arg(ap, long int);
 348        uid = va_arg(ap, int);
 349        gid = va_arg(ap, int);
 350        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsdq",
 351                                    uid, gid, path, mode, rdev);
 352        if (retval > 0) {
 353            header.size = retval;
 354            header.type = T_MKNOD;
 355        }
 356        break;
 357    case T_MKDIR:
 358        path = va_arg(ap, V9fsString *);
 359        mode = va_arg(ap, int);
 360        uid = va_arg(ap, int);
 361        gid = va_arg(ap, int);
 362        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsd",
 363                                    uid, gid, path, mode);
 364        if (retval > 0) {
 365            header.size = retval;
 366            header.type = T_MKDIR;
 367        }
 368        break;
 369    case T_SYMLINK:
 370        oldpath = va_arg(ap, V9fsString *);
 371        path = va_arg(ap, V9fsString *);
 372        uid = va_arg(ap, int);
 373        gid = va_arg(ap, int);
 374        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddss",
 375                                    uid, gid, oldpath, path);
 376        if (retval > 0) {
 377            header.size = retval;
 378            header.type = T_SYMLINK;
 379        }
 380        break;
 381    case T_LINK:
 382        oldpath = va_arg(ap, V9fsString *);
 383        path = va_arg(ap, V9fsString *);
 384        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss",
 385                                    oldpath, path);
 386        if (retval > 0) {
 387            header.size = retval;
 388            header.type = T_LINK;
 389        }
 390        break;
 391    case T_LSTAT:
 392        path = va_arg(ap, V9fsString *);
 393        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
 394        if (retval > 0) {
 395            header.size = retval;
 396            header.type = T_LSTAT;
 397        }
 398        break;
 399    case T_READLINK:
 400        path = va_arg(ap, V9fsString *);
 401        size = va_arg(ap, int);
 402        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, size);
 403        if (retval > 0) {
 404            header.size = retval;
 405            header.type = T_READLINK;
 406        }
 407        break;
 408    case T_STATFS:
 409        path = va_arg(ap, V9fsString *);
 410        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
 411        if (retval > 0) {
 412            header.size = retval;
 413            header.type = T_STATFS;
 414        }
 415        break;
 416    case T_CHMOD:
 417        path = va_arg(ap, V9fsString *);
 418        mode = va_arg(ap, int);
 419        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, mode);
 420        if (retval > 0) {
 421            header.size = retval;
 422            header.type = T_CHMOD;
 423        }
 424        break;
 425    case T_CHOWN:
 426        path = va_arg(ap, V9fsString *);
 427        uid = va_arg(ap, int);
 428        gid = va_arg(ap, int);
 429        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdd", path, uid, gid);
 430        if (retval > 0) {
 431            header.size = retval;
 432            header.type = T_CHOWN;
 433        }
 434        break;
 435    case T_TRUNCATE:
 436        path = va_arg(ap, V9fsString *);
 437        offset = va_arg(ap, uint64_t);
 438        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sq", path, offset);
 439        if (retval > 0) {
 440            header.size = retval;
 441            header.type = T_TRUNCATE;
 442        }
 443        break;
 444    case T_UTIME:
 445        path = va_arg(ap, V9fsString *);
 446        spec[0].tv_sec = va_arg(ap, long);
 447        spec[0].tv_nsec = va_arg(ap, long);
 448        spec[1].tv_sec = va_arg(ap, long);
 449        spec[1].tv_nsec = va_arg(ap, long);
 450        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sqqqq", path,
 451                                    spec[0].tv_sec, spec[1].tv_nsec,
 452                                    spec[1].tv_sec, spec[1].tv_nsec);
 453        if (retval > 0) {
 454            header.size = retval;
 455            header.type = T_UTIME;
 456        }
 457        break;
 458    case T_RENAME:
 459        oldpath = va_arg(ap, V9fsString *);
 460        path = va_arg(ap, V9fsString *);
 461        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", oldpath, path);
 462        if (retval > 0) {
 463            header.size = retval;
 464            header.type = T_RENAME;
 465        }
 466        break;
 467    case T_REMOVE:
 468        path = va_arg(ap, V9fsString *);
 469        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
 470        if (retval > 0) {
 471            header.size = retval;
 472            header.type = T_REMOVE;
 473        }
 474        break;
 475    case T_LGETXATTR:
 476        size = va_arg(ap, int);
 477        path = va_arg(ap, V9fsString *);
 478        name = va_arg(ap, V9fsString *);
 479        retval = proxy_marshal(iovec, PROXY_HDR_SZ,
 480                                    "dss", size, path, name);
 481        if (retval > 0) {
 482            header.size = retval;
 483            header.type = T_LGETXATTR;
 484        }
 485        break;
 486    case T_LLISTXATTR:
 487        size = va_arg(ap, int);
 488        path = va_arg(ap, V9fsString *);
 489        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ds", size, path);
 490        if (retval > 0) {
 491            header.size = retval;
 492            header.type = T_LLISTXATTR;
 493        }
 494        break;
 495    case T_LSETXATTR:
 496        path = va_arg(ap, V9fsString *);
 497        name = va_arg(ap, V9fsString *);
 498        value = va_arg(ap, V9fsString *);
 499        size = va_arg(ap, int);
 500        flags = va_arg(ap, int);
 501        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sssdd",
 502                                    path, name, value, size, flags);
 503        if (retval > 0) {
 504            header.size = retval;
 505            header.type = T_LSETXATTR;
 506        }
 507        break;
 508    case T_LREMOVEXATTR:
 509        path = va_arg(ap, V9fsString *);
 510        name = va_arg(ap, V9fsString *);
 511        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", path, name);
 512        if (retval > 0) {
 513            header.size = retval;
 514            header.type = T_LREMOVEXATTR;
 515        }
 516        break;
 517    case T_GETVERSION:
 518        path = va_arg(ap, V9fsString *);
 519        retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
 520        if (retval > 0) {
 521            header.size = retval;
 522            header.type = T_GETVERSION;
 523        }
 524        break;
 525    default:
 526        error_report("Invalid type %d", type);
 527        retval = -EINVAL;
 528        break;
 529    }
 530    va_end(ap);
 531
 532    if (retval < 0) {
 533        goto err_out;
 534    }
 535
 536    /* marshal the header details */
 537    proxy_marshal(iovec, 0, "dd", header.type, header.size);
 538    header.size += PROXY_HDR_SZ;
 539
 540    retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
 541    if (retval != header.size) {
 542        goto close_error;
 543    }
 544
 545    switch (type) {
 546    case T_OPEN:
 547    case T_CREATE:
 548        /*
 549         * A file descriptor is returned as response for
 550         * T_OPEN,T_CREATE on success
 551         */
 552        if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
 553            goto close_error;
 554        }
 555        break;
 556    case T_MKNOD:
 557    case T_MKDIR:
 558    case T_SYMLINK:
 559    case T_LINK:
 560    case T_CHMOD:
 561    case T_CHOWN:
 562    case T_RENAME:
 563    case T_TRUNCATE:
 564    case T_UTIME:
 565    case T_REMOVE:
 566    case T_LSETXATTR:
 567    case T_LREMOVEXATTR:
 568        if (v9fs_receive_status(proxy, reply, &retval) < 0) {
 569            goto close_error;
 570        }
 571        break;
 572    case T_LSTAT:
 573    case T_READLINK:
 574    case T_STATFS:
 575    case T_GETVERSION:
 576        if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
 577            goto close_error;
 578        }
 579        break;
 580    case T_LGETXATTR:
 581    case T_LLISTXATTR:
 582        if (!size) {
 583            if (v9fs_receive_status(proxy, reply, &retval) < 0) {
 584                goto close_error;
 585            }
 586        } else {
 587            if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
 588                goto close_error;
 589            }
 590        }
 591        break;
 592    }
 593
 594err_out:
 595    qemu_mutex_unlock(&proxy->mutex);
 596    return retval;
 597
 598close_error:
 599    close(proxy->sockfd);
 600    proxy->sockfd = -1;
 601    qemu_mutex_unlock(&proxy->mutex);
 602    return -EIO;
 603}
 604
 605static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
 606{
 607    int retval;
 608    retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, "s", fs_path);
 609    if (retval < 0) {
 610        errno = -retval;
 611        return -1;
 612    }
 613    return retval;
 614}
 615
 616static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
 617                              char *buf, size_t bufsz)
 618{
 619    int retval;
 620    retval = v9fs_request(fs_ctx->private, T_READLINK, buf, "sd",
 621                          fs_path, bufsz);
 622    if (retval < 0) {
 623        errno = -retval;
 624        return -1;
 625    }
 626    return strlen(buf);
 627}
 628
 629static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
 630{
 631    return close(fs->fd);
 632}
 633
 634static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
 635{
 636    return closedir(fs->dir);
 637}
 638
 639static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
 640                      int flags, V9fsFidOpenState *fs)
 641{
 642    fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, flags);
 643    if (fs->fd < 0) {
 644        errno = -fs->fd;
 645        fs->fd = -1;
 646    }
 647    return fs->fd;
 648}
 649
 650static int proxy_opendir(FsContext *ctx,
 651                         V9fsPath *fs_path, V9fsFidOpenState *fs)
 652{
 653    int serrno, fd;
 654
 655    fs->dir = NULL;
 656    fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY);
 657    if (fd < 0) {
 658        errno = -fd;
 659        return -1;
 660    }
 661    fs->dir = fdopendir(fd);
 662    if (!fs->dir) {
 663        serrno = errno;
 664        close(fd);
 665        errno = serrno;
 666        return -1;
 667    }
 668    return 0;
 669}
 670
 671static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
 672{
 673    rewinddir(fs->dir);
 674}
 675
 676static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
 677{
 678    return telldir(fs->dir);
 679}
 680
 681static int proxy_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
 682                           struct dirent *entry,
 683                           struct dirent **result)
 684{
 685    return readdir_r(fs->dir, entry, result);
 686}
 687
 688static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
 689{
 690    seekdir(fs->dir, off);
 691}
 692
 693static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
 694                            const struct iovec *iov,
 695                            int iovcnt, off_t offset)
 696{
 697    ssize_t ret;
 698#ifdef CONFIG_PREADV
 699    ret = preadv(fs->fd, iov, iovcnt, offset);
 700#else
 701    ret = lseek(fs->fd, offset, SEEK_SET);
 702    if (ret >= 0) {
 703        ret = readv(fs->fd, iov, iovcnt);
 704    }
 705#endif
 706    return ret;
 707}
 708
 709static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
 710                             const struct iovec *iov,
 711                             int iovcnt, off_t offset)
 712{
 713    ssize_t ret;
 714
 715#ifdef CONFIG_PREADV
 716    ret = pwritev(fs->fd, iov, iovcnt, offset);
 717#else
 718    ret = lseek(fs->fd, offset, SEEK_SET);
 719    if (ret >= 0) {
 720        ret = writev(fs->fd, iov, iovcnt);
 721    }
 722#endif
 723#ifdef CONFIG_SYNC_FILE_RANGE
 724    if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
 725        /*
 726         * Initiate a writeback. This is not a data integrity sync.
 727         * We want to ensure that we don't leave dirty pages in the cache
 728         * after write when writeout=immediate is sepcified.
 729         */
 730        sync_file_range(fs->fd, offset, ret,
 731                        SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
 732    }
 733#endif
 734    return ret;
 735}
 736
 737static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
 738{
 739    int retval;
 740    retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, "sd",
 741                          fs_path, credp->fc_mode);
 742    if (retval < 0) {
 743        errno = -retval;
 744    }
 745    return retval;
 746}
 747
 748static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
 749                       const char *name, FsCred *credp)
 750{
 751    int retval;
 752    V9fsString fullname;
 753
 754    v9fs_string_init(&fullname);
 755    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
 756
 757    retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, "sdqdd",
 758                          &fullname, credp->fc_mode, credp->fc_rdev,
 759                          credp->fc_uid, credp->fc_gid);
 760    v9fs_string_free(&fullname);
 761    if (retval < 0) {
 762        errno = -retval;
 763        retval = -1;
 764    }
 765    return retval;
 766}
 767
 768static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
 769                       const char *name, FsCred *credp)
 770{
 771    int retval;
 772    V9fsString fullname;
 773
 774    v9fs_string_init(&fullname);
 775    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
 776
 777    retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, "sddd", &fullname,
 778                          credp->fc_mode, credp->fc_uid, credp->fc_gid);
 779    v9fs_string_free(&fullname);
 780    if (retval < 0) {
 781        errno = -retval;
 782        retval = -1;
 783    }
 784    v9fs_string_free(&fullname);
 785    return retval;
 786}
 787
 788static int proxy_fstat(FsContext *fs_ctx, int fid_type,
 789                       V9fsFidOpenState *fs, struct stat *stbuf)
 790{
 791    int fd;
 792
 793    if (fid_type == P9_FID_DIR) {
 794        fd = dirfd(fs->dir);
 795    } else {
 796        fd = fs->fd;
 797    }
 798    return fstat(fd, stbuf);
 799}
 800
 801static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
 802                       int flags, FsCred *credp, V9fsFidOpenState *fs)
 803{
 804    V9fsString fullname;
 805
 806    v9fs_string_init(&fullname);
 807    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
 808
 809    fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, "sdddd",
 810                          &fullname, flags, credp->fc_mode,
 811                          credp->fc_uid, credp->fc_gid);
 812    v9fs_string_free(&fullname);
 813    if (fs->fd < 0) {
 814        errno = -fs->fd;
 815        fs->fd = -1;
 816    }
 817    return fs->fd;
 818}
 819
 820static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
 821                         V9fsPath *dir_path, const char *name, FsCred *credp)
 822{
 823    int retval;
 824    V9fsString fullname, target;
 825
 826    v9fs_string_init(&fullname);
 827    v9fs_string_init(&target);
 828
 829    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
 830    v9fs_string_sprintf(&target, "%s", oldpath);
 831
 832    retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, "ssdd",
 833                          &target, &fullname, credp->fc_uid, credp->fc_gid);
 834    v9fs_string_free(&fullname);
 835    v9fs_string_free(&target);
 836    if (retval < 0) {
 837        errno = -retval;
 838        retval = -1;
 839    }
 840    return retval;
 841}
 842
 843static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
 844                      V9fsPath *dirpath, const char *name)
 845{
 846    int retval;
 847    V9fsString newpath;
 848
 849    v9fs_string_init(&newpath);
 850    v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
 851
 852    retval = v9fs_request(ctx->private, T_LINK, NULL, "ss", oldpath, &newpath);
 853    v9fs_string_free(&newpath);
 854    if (retval < 0) {
 855        errno = -retval;
 856        retval = -1;
 857    }
 858    return retval;
 859}
 860
 861static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
 862{
 863    int retval;
 864
 865    retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, "sq", fs_path, size);
 866    if (retval < 0) {
 867        errno = -retval;
 868        return -1;
 869    }
 870    return 0;
 871}
 872
 873static int proxy_rename(FsContext *ctx, const char *oldpath,
 874                        const char *newpath)
 875{
 876    int retval;
 877    V9fsString oldname, newname;
 878
 879    v9fs_string_init(&oldname);
 880    v9fs_string_init(&newname);
 881
 882    v9fs_string_sprintf(&oldname, "%s", oldpath);
 883    v9fs_string_sprintf(&newname, "%s", newpath);
 884    retval = v9fs_request(ctx->private, T_RENAME, NULL, "ss",
 885                          &oldname, &newname);
 886    v9fs_string_free(&oldname);
 887    v9fs_string_free(&newname);
 888    if (retval < 0) {
 889        errno = -retval;
 890    }
 891    return retval;
 892}
 893
 894static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
 895{
 896    int retval;
 897    retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, "sdd",
 898                          fs_path, credp->fc_uid, credp->fc_gid);
 899    if (retval < 0) {
 900        errno = -retval;
 901    }
 902    return retval;
 903}
 904
 905static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
 906                           const struct timespec *buf)
 907{
 908    int retval;
 909    retval = v9fs_request(s->private, T_UTIME, NULL, "sqqqq",
 910                          fs_path,
 911                          buf[0].tv_sec, buf[0].tv_nsec,
 912                          buf[1].tv_sec, buf[1].tv_nsec);
 913    if (retval < 0) {
 914        errno = -retval;
 915    }
 916    return retval;
 917}
 918
 919static int proxy_remove(FsContext *ctx, const char *path)
 920{
 921    int retval;
 922    V9fsString name;
 923    v9fs_string_init(&name);
 924    v9fs_string_sprintf(&name, "%s", path);
 925    retval = v9fs_request(ctx->private, T_REMOVE, NULL, "s", &name);
 926    v9fs_string_free(&name);
 927    if (retval < 0) {
 928        errno = -retval;
 929    }
 930    return retval;
 931}
 932
 933static int proxy_fsync(FsContext *ctx, int fid_type,
 934                       V9fsFidOpenState *fs, int datasync)
 935{
 936    int fd;
 937
 938    if (fid_type == P9_FID_DIR) {
 939        fd = dirfd(fs->dir);
 940    } else {
 941        fd = fs->fd;
 942    }
 943
 944    if (datasync) {
 945        return qemu_fdatasync(fd);
 946    } else {
 947        return fsync(fd);
 948    }
 949}
 950
 951static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
 952{
 953    int retval;
 954    retval = v9fs_request(s->private, T_STATFS, stbuf, "s", fs_path);
 955    if (retval < 0) {
 956        errno = -retval;
 957        return -1;
 958    }
 959    return retval;
 960}
 961
 962static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
 963                               const char *name, void *value, size_t size)
 964{
 965    int retval;
 966    V9fsString xname;
 967
 968    v9fs_string_init(&xname);
 969    v9fs_string_sprintf(&xname, "%s", name);
 970    retval = v9fs_request(ctx->private, T_LGETXATTR, value, "dss", size,
 971                          fs_path, &xname);
 972    v9fs_string_free(&xname);
 973    if (retval < 0) {
 974        errno = -retval;
 975    }
 976    return retval;
 977}
 978
 979static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
 980                                void *value, size_t size)
 981{
 982    int retval;
 983    retval = v9fs_request(ctx->private, T_LLISTXATTR, value, "ds", size,
 984                        fs_path);
 985    if (retval < 0) {
 986        errno = -retval;
 987    }
 988    return retval;
 989}
 990
 991static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
 992                           void *value, size_t size, int flags)
 993{
 994    int retval;
 995    V9fsString xname, xvalue;
 996
 997    v9fs_string_init(&xname);
 998    v9fs_string_sprintf(&xname, "%s", name);
 999
1000    v9fs_string_init(&xvalue);
1001    xvalue.size = size;
1002    xvalue.data = g_malloc(size);
1003    memcpy(xvalue.data, value, size);
1004
1005    retval = v9fs_request(ctx->private, T_LSETXATTR, value, "sssdd",
1006                          fs_path, &xname, &xvalue, size, flags);
1007    v9fs_string_free(&xname);
1008    v9fs_string_free(&xvalue);
1009    if (retval < 0) {
1010        errno = -retval;
1011    }
1012    return retval;
1013}
1014
1015static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1016                              const char *name)
1017{
1018    int retval;
1019    V9fsString xname;
1020
1021    v9fs_string_init(&xname);
1022    v9fs_string_sprintf(&xname, "%s", name);
1023    retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, "ss",
1024                          fs_path, &xname);
1025    v9fs_string_free(&xname);
1026    if (retval < 0) {
1027        errno = -retval;
1028    }
1029    return retval;
1030}
1031
1032static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1033                              const char *name, V9fsPath *target)
1034{
1035    if (dir_path) {
1036        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
1037                            dir_path->data, name);
1038    } else {
1039        v9fs_string_sprintf((V9fsString *)target, "%s", name);
1040    }
1041    /* Bump the size for including terminating NULL */
1042    target->size++;
1043    return 0;
1044}
1045
1046static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
1047                          const char *old_name, V9fsPath *newdir,
1048                          const char *new_name)
1049{
1050    int ret;
1051    V9fsString old_full_name, new_full_name;
1052
1053    v9fs_string_init(&old_full_name);
1054    v9fs_string_init(&new_full_name);
1055
1056    v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1057    v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1058
1059    ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
1060    v9fs_string_free(&old_full_name);
1061    v9fs_string_free(&new_full_name);
1062    return ret;
1063}
1064
1065static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
1066                          const char *name, int flags)
1067{
1068    int ret;
1069    V9fsString fullname;
1070    v9fs_string_init(&fullname);
1071
1072    v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1073    ret = proxy_remove(ctx, fullname.data);
1074    v9fs_string_free(&fullname);
1075
1076    return ret;
1077}
1078
1079static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
1080                                mode_t st_mode, uint64_t *st_gen)
1081{
1082    int err;
1083
1084    /* Do not try to open special files like device nodes, fifos etc
1085     * we can get fd for regular files and directories only
1086     */
1087    if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1088        errno = ENOTTY;
1089        return -1;
1090    }
1091    err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, "s", path);
1092    if (err < 0) {
1093        errno = -err;
1094        err = -1;
1095    }
1096    return err;
1097}
1098
1099static int connect_namedsocket(const char *path)
1100{
1101    int sockfd, size;
1102    struct sockaddr_un helper;
1103
1104    if (strlen(path) >= sizeof(helper.sun_path)) {
1105        error_report("Socket name too long");
1106        return -1;
1107    }
1108    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1109    if (sockfd < 0) {
1110        error_report("Failed to create socket: %s", strerror(errno));
1111        return -1;
1112    }
1113    strcpy(helper.sun_path, path);
1114    helper.sun_family = AF_UNIX;
1115    size = strlen(helper.sun_path) + sizeof(helper.sun_family);
1116    if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
1117        error_report("Failed to connect to %s: %s", path, strerror(errno));
1118        close(sockfd);
1119        return -1;
1120    }
1121
1122    /* remove the socket for security reasons */
1123    unlink(path);
1124    return sockfd;
1125}
1126
1127static int proxy_parse_opts(QemuOpts *opts, struct FsDriverEntry *fs)
1128{
1129    const char *socket = qemu_opt_get(opts, "socket");
1130    const char *sock_fd = qemu_opt_get(opts, "sock_fd");
1131
1132    if (!socket && !sock_fd) {
1133        error_report("Must specify either socket or sock_fd");
1134        return -1;
1135    }
1136    if (socket && sock_fd) {
1137        error_report("Both socket and sock_fd options specified");
1138        return -1;
1139    }
1140    if (socket) {
1141        fs->path = g_strdup(socket);
1142        fs->export_flags = V9FS_PROXY_SOCK_NAME;
1143    } else {
1144        fs->path = g_strdup(sock_fd);
1145        fs->export_flags = V9FS_PROXY_SOCK_FD;
1146    }
1147    return 0;
1148}
1149
1150static int proxy_init(FsContext *ctx)
1151{
1152    V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy));
1153    int sock_id;
1154
1155    if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1156        sock_id = connect_namedsocket(ctx->fs_root);
1157    } else {
1158        sock_id = atoi(ctx->fs_root);
1159        if (sock_id < 0) {
1160            error_report("Socket descriptor not initialized");
1161        }
1162    }
1163    if (sock_id < 0) {
1164        g_free(proxy);
1165        return -1;
1166    }
1167    g_free(ctx->fs_root);
1168    ctx->fs_root = NULL;
1169
1170    proxy->in_iovec.iov_base  = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1171    proxy->in_iovec.iov_len   = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1172    proxy->out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1173    proxy->out_iovec.iov_len  = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1174
1175    ctx->private = proxy;
1176    proxy->sockfd = sock_id;
1177    qemu_mutex_init(&proxy->mutex);
1178
1179    ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1180    ctx->exops.get_st_gen = proxy_ioc_getversion;
1181    return 0;
1182}
1183
1184FileOperations proxy_ops = {
1185    .parse_opts   = proxy_parse_opts,
1186    .init         = proxy_init,
1187    .lstat        = proxy_lstat,
1188    .readlink     = proxy_readlink,
1189    .close        = proxy_close,
1190    .closedir     = proxy_closedir,
1191    .open         = proxy_open,
1192    .opendir      = proxy_opendir,
1193    .rewinddir    = proxy_rewinddir,
1194    .telldir      = proxy_telldir,
1195    .readdir_r    = proxy_readdir_r,
1196    .seekdir      = proxy_seekdir,
1197    .preadv       = proxy_preadv,
1198    .pwritev      = proxy_pwritev,
1199    .chmod        = proxy_chmod,
1200    .mknod        = proxy_mknod,
1201    .mkdir        = proxy_mkdir,
1202    .fstat        = proxy_fstat,
1203    .open2        = proxy_open2,
1204    .symlink      = proxy_symlink,
1205    .link         = proxy_link,
1206    .truncate     = proxy_truncate,
1207    .rename       = proxy_rename,
1208    .chown        = proxy_chown,
1209    .utimensat    = proxy_utimensat,
1210    .remove       = proxy_remove,
1211    .fsync        = proxy_fsync,
1212    .statfs       = proxy_statfs,
1213    .lgetxattr    = proxy_lgetxattr,
1214    .llistxattr   = proxy_llistxattr,
1215    .lsetxattr    = proxy_lsetxattr,
1216    .lremovexattr = proxy_lremovexattr,
1217    .name_to_path = proxy_name_to_path,
1218    .renameat     = proxy_renameat,
1219    .unlinkat     = proxy_unlinkat,
1220};
1221