qemu-fsdev.c 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Virtio 9p
 *
 * Copyright IBM, Corp. 2010
 *
 * Authors:
 *  Gautham R Shenoy <ego@in.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */
#include <stdio.h>
#include <string.h>
#include "qemu-fsdev.h"
16 17
#include "qemu/queue.h"
#include "qemu/osdep.h"
18
#include "qemu-common.h"
19
#include "qemu/config-file.h"
20

21 22
static QTAILQ_HEAD(FsDriverEntry_head, FsDriverListEntry) fsdriver_entries =
    QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
23

24
static FsDriverTable FsDrivers[] = {
25
    { .name = "local", .ops = &local_ops},
26
#ifdef CONFIG_OPEN_BY_HANDLE
27
    { .name = "handle", .ops = &handle_ops},
28
#endif
29
    { .name = "synth", .ops = &synth_ops},
30
    { .name = "proxy", .ops = &proxy_ops},
31 32 33 34 35
};

int qemu_fsdev_add(QemuOpts *opts)
{
    int i;
36
    struct FsDriverListEntry *fsle;
37
    const char *fsdev_id = qemu_opts_id(opts);
38
    const char *fsdriver = qemu_opt_get(opts, "fsdriver");
39
    const char *writeout = qemu_opt_get(opts, "writeout");
40
    bool ro = qemu_opt_get_bool(opts, "readonly", 0);
41

42
    if (!fsdev_id) {
43 44 45 46
        fprintf(stderr, "fsdev: No id specified\n");
        return -1;
    }

47 48 49
    if (fsdriver) {
        for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
            if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
50 51
                break;
            }
52 53
        }

54 55
        if (i == ARRAY_SIZE(FsDrivers)) {
            fprintf(stderr, "fsdev: fsdriver %s not found\n", fsdriver);
56 57 58
            return -1;
        }
    } else {
59
        fprintf(stderr, "fsdev: No fsdriver specified\n");
60 61 62
        return -1;
    }

63
    fsle = g_malloc0(sizeof(*fsle));
64
    fsle->fse.fsdev_id = g_strdup(fsdev_id);
65
    fsle->fse.ops = FsDrivers[i].ops;
66 67
    if (writeout) {
        if (!strcmp(writeout, "immediate")) {
68
            fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
69 70
        }
    }
71 72 73 74 75
    if (ro) {
        fsle->fse.export_flags |= V9FS_RDONLY;
    } else {
        fsle->fse.export_flags &= ~V9FS_RDONLY;
    }
76

77 78
    if (fsle->fse.ops->parse_opts) {
        if (fsle->fse.ops->parse_opts(opts, &fsle->fse)) {
S
Stefan Weil 已提交
79 80
            g_free(fsle->fse.fsdev_id);
            g_free(fsle);
81 82
            return -1;
        }
83 84
    }

85
    QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
86 87 88
    return 0;
}

89
FsDriverEntry *get_fsdev_fsentry(char *id)
90
{
91
    if (id) {
92
        struct FsDriverListEntry *fsle;
93

94
        QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
95 96 97
            if (strcmp(fsle->fse.fsdev_id, id) == 0) {
                return &fsle->fse;
            }
98 99 100 101
        }
    }
    return NULL;
}