9p-synth.c 16.2 KB
Newer Older
1
/*
2
 * 9p synthetic file system support
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Malahal Naineni <malahal@us.ibm.com>
 *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.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 已提交
15
#include "qemu/osdep.h"
W
Wei Liu 已提交
16
#include "9p.h"
17
#include "fsdev/qemu-fsdev.h"
18
#include "9p-synth.h"
P
Paolo Bonzini 已提交
19
#include "qemu/rcu.h"
M
Mike Day 已提交
20
#include "qemu/rcu_queue.h"
21
#include "qemu/cutils.h"
22
#include "sysemu/qtest.h"
23 24

/* Root node for synth file system */
G
Greg Kurz 已提交
25
static V9fsSynthNode synth_root = {
26 27 28 29 30
    .name = "/",
    .actual_attr = {
        .mode = 0555 | S_IFDIR,
        .nlink = 1,
    },
G
Greg Kurz 已提交
31
    .attr = &synth_root.actual_attr,
32 33
};

G
Greg Kurz 已提交
34 35
static QemuMutex  synth_mutex;
static int synth_node_count;
36
/* set to 1 when the synth fs is ready */
G
Greg Kurz 已提交
37
static int synth_fs;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
                                        const char *name,
                                        V9fsSynthNodeAttr *attr, int inode)
{
    V9fsSynthNode *node;

    /* Add directory type and remove write bits */
    mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
    node = g_malloc0(sizeof(V9fsSynthNode));
    if (attr) {
        /* We are adding .. or . entries */
        node->attr = attr;
        node->attr->nlink++;
    } else {
        node->attr = &node->actual_attr;
        node->attr->inode = inode;
        node->attr->nlink = 1;
        /* We don't allow write to directories */
        node->attr->mode   = mode;
        node->attr->write = NULL;
        node->attr->read  = NULL;
    }
    node->private = node;
J
Jim Meyering 已提交
62
    pstrcpy(node->name, sizeof(node->name), name);
63
    QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
64 65 66 67 68 69 70 71 72
    return node;
}

int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
                          const char *name, V9fsSynthNode **result)
{
    int ret;
    V9fsSynthNode *node, *tmp;

G
Greg Kurz 已提交
73
    if (!synth_fs) {
74 75 76 77 78 79
        return EAGAIN;
    }
    if (!name || (strlen(name) >= NAME_MAX)) {
        return EINVAL;
    }
    if (!parent) {
G
Greg Kurz 已提交
80
        parent = &synth_root;
81
    }
G
Greg Kurz 已提交
82
    qemu_mutex_lock(&synth_mutex);
83 84 85 86 87 88 89
    QLIST_FOREACH(tmp, &parent->child, sibling) {
        if (!strcmp(tmp->name, name)) {
            ret = EEXIST;
            goto err_out;
        }
    }
    /* Add the name */
G
Greg Kurz 已提交
90
    node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
91 92 93 94 95 96 97
    v9fs_add_dir_node(node, parent->attr->mode, "..",
                      parent->attr, parent->attr->inode);
    v9fs_add_dir_node(node, node->attr->mode, ".",
                      node->attr, node->attr->inode);
    *result = node;
    ret = 0;
err_out:
G
Greg Kurz 已提交
98
    qemu_mutex_unlock(&synth_mutex);
99 100 101 102 103 104 105 106 107 108
    return ret;
}

int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
                             const char *name, v9fs_synth_read read,
                             v9fs_synth_write write, void *arg)
{
    int ret;
    V9fsSynthNode *node, *tmp;

G
Greg Kurz 已提交
109
    if (!synth_fs) {
110 111 112 113 114 115
        return EAGAIN;
    }
    if (!name || (strlen(name) >= NAME_MAX)) {
        return EINVAL;
    }
    if (!parent) {
G
Greg Kurz 已提交
116
        parent = &synth_root;
117 118
    }

G
Greg Kurz 已提交
119
    qemu_mutex_lock(&synth_mutex);
120 121 122 123 124 125 126 127 128 129
    QLIST_FOREACH(tmp, &parent->child, sibling) {
        if (!strcmp(tmp->name, name)) {
            ret = EEXIST;
            goto err_out;
        }
    }
    /* Add file type and remove write bits */
    mode = ((mode & 0777) | S_IFREG);
    node = g_malloc0(sizeof(V9fsSynthNode));
    node->attr         = &node->actual_attr;
G
Greg Kurz 已提交
130
    node->attr->inode  = synth_node_count++;
131 132 133 134 135
    node->attr->nlink  = 1;
    node->attr->read   = read;
    node->attr->write  = write;
    node->attr->mode   = mode;
    node->private      = arg;
J
Jim Meyering 已提交
136
    pstrcpy(node->name, sizeof(node->name), name);
137
    QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
138 139
    ret = 0;
err_out:
G
Greg Kurz 已提交
140
    qemu_mutex_unlock(&synth_mutex);
141 142 143
    return ret;
}

G
Greg Kurz 已提交
144
static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
    stbuf->st_dev = 0;
    stbuf->st_ino = node->attr->inode;
    stbuf->st_mode = node->attr->mode;
    stbuf->st_nlink = node->attr->nlink;
    stbuf->st_uid = 0;
    stbuf->st_gid = 0;
    stbuf->st_rdev = 0;
    stbuf->st_size = 0;
    stbuf->st_blksize = 0;
    stbuf->st_blocks = 0;
    stbuf->st_atime = 0;
    stbuf->st_mtime = 0;
    stbuf->st_ctime = 0;
}

G
Greg Kurz 已提交
161
static int synth_lstat(FsContext *fs_ctx,
162 163 164 165
                            V9fsPath *fs_path, struct stat *stbuf)
{
    V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;

G
Greg Kurz 已提交
166
    synth_fill_statbuf(node, stbuf);
167 168 169
    return 0;
}

G
Greg Kurz 已提交
170
static int synth_fstat(FsContext *fs_ctx, int fid_type,
171 172 173
                            V9fsFidOpenState *fs, struct stat *stbuf)
{
    V9fsSynthOpenState *synth_open = fs->private;
G
Greg Kurz 已提交
174
    synth_fill_statbuf(synth_open->node, stbuf);
175 176 177
    return 0;
}

G
Greg Kurz 已提交
178
static int synth_opendir(FsContext *ctx,
179 180 181 182 183 184 185 186 187 188 189 190
                             V9fsPath *fs_path, V9fsFidOpenState *fs)
{
    V9fsSynthOpenState *synth_open;
    V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;

    synth_open = g_malloc(sizeof(*synth_open));
    synth_open->node = node;
    node->open_count++;
    fs->private = synth_open;
    return 0;
}

G
Greg Kurz 已提交
191
static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
192 193 194 195 196 197 198 199 200 201
{
    V9fsSynthOpenState *synth_open = fs->private;
    V9fsSynthNode *node = synth_open->node;

    node->open_count--;
    g_free(synth_open);
    fs->private = NULL;
    return 0;
}

G
Greg Kurz 已提交
202
static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
203 204 205 206 207
{
    V9fsSynthOpenState *synth_open = fs->private;
    return synth_open->offset;
}

G
Greg Kurz 已提交
208
static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
209 210 211 212 213
{
    V9fsSynthOpenState *synth_open = fs->private;
    synth_open->offset = off;
}

G
Greg Kurz 已提交
214
static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
215
{
G
Greg Kurz 已提交
216
    synth_seekdir(ctx, fs, 0);
217 218
}

G
Greg Kurz 已提交
219
static void synth_direntry(V9fsSynthNode *node,
220 221 222 223 224 225 226
                                struct dirent *entry, off_t off)
{
    strcpy(entry->d_name, node->name);
    entry->d_ino = node->attr->inode;
    entry->d_off = off + 1;
}

G
Greg Kurz 已提交
227
static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
G
Greg Kurz 已提交
228
                                            struct dirent *entry, off_t off)
229 230 231 232
{
    int i = 0;
    V9fsSynthNode *node;

233
    rcu_read_lock();
234 235 236 237 238 239 240
    QLIST_FOREACH(node, &dir->child, sibling) {
        /* This is the off child of the directory */
        if (i == off) {
            break;
        }
        i++;
    }
241
    rcu_read_unlock();
242 243
    if (!node) {
        /* end of directory */
G
Greg Kurz 已提交
244
        return NULL;
245
    }
G
Greg Kurz 已提交
246
    synth_direntry(node, entry, off);
G
Greg Kurz 已提交
247
    return entry;
248 249
}

G
Greg Kurz 已提交
250
static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
251
{
G
Greg Kurz 已提交
252
    struct dirent *entry;
253 254
    V9fsSynthOpenState *synth_open = fs->private;
    V9fsSynthNode *node = synth_open->node;
G
Greg Kurz 已提交
255
    entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
G
Greg Kurz 已提交
256
    if (entry) {
257 258
        synth_open->offset++;
    }
G
Greg Kurz 已提交
259
    return entry;
260 261
}

G
Greg Kurz 已提交
262
static int synth_open(FsContext *ctx, V9fsPath *fs_path,
263 264 265 266 267 268 269 270 271 272 273 274
                           int flags, V9fsFidOpenState *fs)
{
    V9fsSynthOpenState *synth_open;
    V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;

    synth_open = g_malloc(sizeof(*synth_open));
    synth_open->node = node;
    node->open_count++;
    fs->private = synth_open;
    return 0;
}

G
Greg Kurz 已提交
275
static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
276 277 278 279 280 281 282
                            const char *name, int flags,
                            FsCred *credp, V9fsFidOpenState *fs)
{
    errno = ENOSYS;
    return -1;
}

G
Greg Kurz 已提交
283
static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
284 285 286 287 288 289 290 291 292 293
{
    V9fsSynthOpenState *synth_open = fs->private;
    V9fsSynthNode *node = synth_open->node;

    node->open_count--;
    g_free(synth_open);
    fs->private = NULL;
    return 0;
}

G
Greg Kurz 已提交
294
static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
                                  const struct iovec *iov,
                                  int iovcnt, off_t offset)
{
    int i, count = 0, wcount;
    V9fsSynthOpenState *synth_open = fs->private;
    V9fsSynthNode *node = synth_open->node;
    if (!node->attr->write) {
        errno = EPERM;
        return -1;
    }
    for (i = 0; i < iovcnt; i++) {
        wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
                                   offset, node->private);
        offset += wcount;
        count  += wcount;
        /* If we wrote less than requested. we are done */
        if (wcount < iov[i].iov_len) {
            break;
        }
    }
    return count;
}

G
Greg Kurz 已提交
318
static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                                 const struct iovec *iov,
                                 int iovcnt, off_t offset)
{
    int i, count = 0, rcount;
    V9fsSynthOpenState *synth_open = fs->private;
    V9fsSynthNode *node = synth_open->node;
    if (!node->attr->read) {
        errno = EPERM;
        return -1;
    }
    for (i = 0; i < iovcnt; i++) {
        rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
                                  offset, node->private);
        offset += rcount;
        count  += rcount;
        /* If we read less than requested. we are done */
        if (rcount < iov[i].iov_len) {
            break;
        }
    }
    return count;
}

G
Greg Kurz 已提交
342
static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
343 344 345 346 347
{
    errno = ENOSYS;
    return -1;
}

G
Greg Kurz 已提交
348
static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
349 350 351 352 353
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
354
static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
355 356 357 358 359 360
                       const char *buf, FsCred *credp)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
361
static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
362 363 364 365 366 367
                       const char *buf, FsCred *credp)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
368
static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
369 370 371 372 373 374
                                   char *buf, size_t bufsz)
{
    errno = ENOSYS;
    return -1;
}

G
Greg Kurz 已提交
375
static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
376 377 378 379 380 381
                              V9fsPath *newpath, const char *buf, FsCred *credp)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
382
static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
383 384 385 386 387 388
                           V9fsPath *newpath, const char *buf)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
389
static int synth_rename(FsContext *ctx, const char *oldpath,
390 391 392 393 394 395
                             const char *newpath)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
396
static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
397 398 399 400 401
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
402
static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
403 404 405 406 407 408
                                const struct timespec *buf)
{
    errno = EPERM;
    return 0;
}

G
Greg Kurz 已提交
409
static int synth_remove(FsContext *ctx, const char *path)
410 411 412 413 414
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
415
static int synth_fsync(FsContext *ctx, int fid_type,
416
                            V9fsFidOpenState *fs, int datasync)
417 418 419 420 421
{
    errno = ENOSYS;
    return 0;
}

G
Greg Kurz 已提交
422
static int synth_statfs(FsContext *s, V9fsPath *fs_path,
423 424 425 426 427
                             struct statfs *stbuf)
{
    stbuf->f_type = 0xABCD;
    stbuf->f_bsize = 512;
    stbuf->f_blocks = 0;
G
Greg Kurz 已提交
428
    stbuf->f_files = synth_node_count;
429 430 431 432
    stbuf->f_namelen = NAME_MAX;
    return 0;
}

G
Greg Kurz 已提交
433
static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
434 435 436 437 438 439
                                    const char *name, void *value, size_t size)
{
    errno = ENOTSUP;
    return -1;
}

G
Greg Kurz 已提交
440
static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
441 442 443 444 445 446
                                     void *value, size_t size)
{
    errno = ENOTSUP;
    return -1;
}

G
Greg Kurz 已提交
447
static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
448 449 450 451 452 453 454
                                const char *name, void *value,
                                size_t size, int flags)
{
    errno = ENOTSUP;
    return -1;
}

G
Greg Kurz 已提交
455
static int synth_lremovexattr(FsContext *ctx,
456 457 458 459 460 461
                                   V9fsPath *path, const char *name)
{
    errno = ENOTSUP;
    return -1;
}

G
Greg Kurz 已提交
462
static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
463 464 465 466 467 468 469 470 471 472 473 474
                                   const char *name, V9fsPath *target)
{
    V9fsSynthNode *node;
    V9fsSynthNode *dir_node;

    /* "." and ".." are not allowed */
    if (!strcmp(name, ".") || !strcmp(name, "..")) {
        errno = EINVAL;
        return -1;

    }
    if (!dir_path) {
G
Greg Kurz 已提交
475
        dir_node = &synth_root;
476 477 478 479 480 481 482 483
    } else {
        dir_node = *(V9fsSynthNode **)dir_path->data;
    }
    if (!strcmp(name, "/")) {
        node = dir_node;
        goto out;
    }
    /* search for the name in the childern */
484
    rcu_read_lock();
485 486 487 488 489
    QLIST_FOREACH(node, &dir_node->child, sibling) {
        if (!strcmp(node->name, name)) {
            break;
        }
    }
490 491
    rcu_read_unlock();

492 493 494 495 496 497
    if (!node) {
        errno = ENOENT;
        return -1;
    }
out:
    /* Copy the node pointer to fid */
498
    target->data = g_memdup(&node, sizeof(void *));
499 500 501 502
    target->size = sizeof(void *);
    return 0;
}

G
Greg Kurz 已提交
503
static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
504 505 506 507 508 509 510
                               const char *old_name, V9fsPath *newdir,
                               const char *new_name)
{
    errno = EPERM;
    return -1;
}

G
Greg Kurz 已提交
511
static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
512 513 514 515 516 517
                               const char *name, int flags)
{
    errno = EPERM;
    return -1;
}

518 519 520 521 522 523
static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
                                      void *arg)
{
    return 1;
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537
static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
                                            void *arg)
{
    bool should_block = !!*(uint8_t *)buf;

    if (should_block) {
        /* This will cause the server to call us again until we're cancelled */
        errno = EINTR;
        return -1;
    }

    return 1;
}

538
static int synth_init(FsContext *ctx, Error **errp)
539
{
G
Greg Kurz 已提交
540 541
    QLIST_INIT(&synth_root.child);
    qemu_mutex_init(&synth_mutex);
542 543

    /* Add "." and ".." entries for root */
G
Greg Kurz 已提交
544 545 546 547
    v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
                      "..", synth_root.attr, synth_root.attr->inode);
    v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
                      ".", synth_root.attr, synth_root.attr->inode);
548 549

    /* Mark the subsystem is ready for use */
G
Greg Kurz 已提交
550
    synth_fs = 1;
551 552 553 554 555 556 557 558 559 560 561 562 563

    if (qtest_enabled()) {
        V9fsSynthNode *node = NULL;
        int i, ret;

        /* Directory hierarchy for WALK test */
        for (i = 0; i < P9_MAXWELEM; i++) {
            char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);

            ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
            assert(!ret);
            g_free(name);
        }
564 565 566 567 568

        /* File for LOPEN test */
        ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
                                       NULL, NULL, ctx);
        assert(!ret);
569 570 571 572 573

        /* File for WRITE test */
        ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
                                       NULL, v9fs_synth_qtest_write, ctx);
        assert(!ret);
574 575 576 577 578 579

        /* File for FLUSH test */
        ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
                                       NULL, v9fs_synth_qtest_flush_write,
                                       ctx);
        assert(!ret);
580 581
    }

582 583 584 585
    return 0;
}

FileOperations synth_ops = {
G
Greg Kurz 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    .init         = synth_init,
    .lstat        = synth_lstat,
    .readlink     = synth_readlink,
    .close        = synth_close,
    .closedir     = synth_closedir,
    .open         = synth_open,
    .opendir      = synth_opendir,
    .rewinddir    = synth_rewinddir,
    .telldir      = synth_telldir,
    .readdir      = synth_readdir,
    .seekdir      = synth_seekdir,
    .preadv       = synth_preadv,
    .pwritev      = synth_pwritev,
    .chmod        = synth_chmod,
    .mknod        = synth_mknod,
    .mkdir        = synth_mkdir,
    .fstat        = synth_fstat,
    .open2        = synth_open2,
    .symlink      = synth_symlink,
    .link         = synth_link,
    .truncate     = synth_truncate,
    .rename       = synth_rename,
    .chown        = synth_chown,
    .utimensat    = synth_utimensat,
    .remove       = synth_remove,
    .fsync        = synth_fsync,
    .statfs       = synth_statfs,
    .lgetxattr    = synth_lgetxattr,
    .llistxattr   = synth_llistxattr,
    .lsetxattr    = synth_lsetxattr,
    .lremovexattr = synth_lremovexattr,
    .name_to_path = synth_name_to_path,
    .renameat     = synth_renameat,
    .unlinkat     = synth_unlinkat,
620
};