174db920cSGautham R Shenoy /*
2af8b38b0SGreg Kurz * 9p
374db920cSGautham R Shenoy *
474db920cSGautham R Shenoy * Copyright IBM, Corp. 2010
574db920cSGautham R Shenoy *
674db920cSGautham R Shenoy * Authors:
774db920cSGautham R Shenoy * Gautham R Shenoy <ego@in.ibm.com>
874db920cSGautham R Shenoy *
974db920cSGautham R Shenoy * This work is licensed under the terms of the GNU GPL, version 2. See
1074db920cSGautham R Shenoy * the COPYING file in the top-level directory.
1174db920cSGautham R Shenoy */
12e688df6bSMarkus Armbruster
13fbc04127SPeter Maydell #include "qemu/osdep.h"
14e688df6bSMarkus Armbruster #include "qapi/error.h"
1574db920cSGautham R Shenoy #include "qemu-fsdev.h"
161de7afc9SPaolo Bonzini #include "qemu/queue.h"
171de7afc9SPaolo Bonzini #include "qemu/config-file.h"
18ea753f32SGreg Kurz #include "qemu/error-report.h"
19922a01a0SMarkus Armbruster #include "qemu/option.h"
2074db920cSGautham R Shenoy
2120232435SGreg Kurz /*
2220232435SGreg Kurz * A table to store the various file systems and their callback operations.
2320232435SGreg Kurz * -----------------
2420232435SGreg Kurz * fstype | ops
2520232435SGreg Kurz * -----------------
2620232435SGreg Kurz * local | local_ops
2720232435SGreg Kurz * . |
2820232435SGreg Kurz * . |
2920232435SGreg Kurz * . |
3020232435SGreg Kurz * . |
3120232435SGreg Kurz * -----------------
3220232435SGreg Kurz * etc
3320232435SGreg Kurz */
3420232435SGreg Kurz typedef struct FsDriverTable {
3520232435SGreg Kurz const char *name;
3620232435SGreg Kurz FileOperations *ops;
37aee7f3ecSGreg Kurz const char **opts;
3820232435SGreg Kurz } FsDriverTable;
3920232435SGreg Kurz
4020232435SGreg Kurz typedef struct FsDriverListEntry {
4120232435SGreg Kurz FsDriverEntry fse;
4220232435SGreg Kurz QTAILQ_ENTRY(FsDriverListEntry) next;
4320232435SGreg Kurz } FsDriverListEntry;
4420232435SGreg Kurz
45b58deb34SPaolo Bonzini static QTAILQ_HEAD(, FsDriverListEntry) fsdriver_entries =
46fbcbf101SAneesh Kumar K.V QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
4774db920cSGautham R Shenoy
48aee7f3ecSGreg Kurz #define COMMON_FS_DRIVER_OPTIONS "id", "fsdriver", "readonly"
49aee7f3ecSGreg Kurz
50fbcbf101SAneesh Kumar K.V static FsDriverTable FsDrivers[] = {
51aee7f3ecSGreg Kurz {
52aee7f3ecSGreg Kurz .name = "local",
53aee7f3ecSGreg Kurz .ops = &local_ops,
54aee7f3ecSGreg Kurz .opts = (const char * []) {
55aee7f3ecSGreg Kurz COMMON_FS_DRIVER_OPTIONS,
56aee7f3ecSGreg Kurz "security_model",
57aee7f3ecSGreg Kurz "path",
58aee7f3ecSGreg Kurz "writeout",
59aee7f3ecSGreg Kurz "fmode",
60aee7f3ecSGreg Kurz "dmode",
611a6ed33cSAntonios Motakis "multidevs",
62aee7f3ecSGreg Kurz "throttling.bps-total",
63aee7f3ecSGreg Kurz "throttling.bps-read",
64aee7f3ecSGreg Kurz "throttling.bps-write",
65aee7f3ecSGreg Kurz "throttling.iops-total",
66aee7f3ecSGreg Kurz "throttling.iops-read",
67aee7f3ecSGreg Kurz "throttling.iops-write",
68aee7f3ecSGreg Kurz "throttling.bps-total-max",
69aee7f3ecSGreg Kurz "throttling.bps-read-max",
70aee7f3ecSGreg Kurz "throttling.bps-write-max",
71aee7f3ecSGreg Kurz "throttling.iops-total-max",
72aee7f3ecSGreg Kurz "throttling.iops-read-max",
73aee7f3ecSGreg Kurz "throttling.iops-write-max",
74aee7f3ecSGreg Kurz "throttling.bps-total-max-length",
75aee7f3ecSGreg Kurz "throttling.bps-read-max-length",
76aee7f3ecSGreg Kurz "throttling.bps-write-max-length",
77aee7f3ecSGreg Kurz "throttling.iops-total-max-length",
78aee7f3ecSGreg Kurz "throttling.iops-read-max-length",
79aee7f3ecSGreg Kurz "throttling.iops-write-max-length",
80aee7f3ecSGreg Kurz "throttling.iops-size",
81*353b5a91SPrasad J Pandit NULL
82aee7f3ecSGreg Kurz },
83aee7f3ecSGreg Kurz },
84aee7f3ecSGreg Kurz {
85aee7f3ecSGreg Kurz .name = "synth",
86aee7f3ecSGreg Kurz .ops = &synth_ops,
87aee7f3ecSGreg Kurz .opts = (const char * []) {
88aee7f3ecSGreg Kurz COMMON_FS_DRIVER_OPTIONS,
89*353b5a91SPrasad J Pandit NULL
90aee7f3ecSGreg Kurz },
91aee7f3ecSGreg Kurz },
9274db920cSGautham R Shenoy };
9374db920cSGautham R Shenoy
validate_opt(void * opaque,const char * name,const char * value,Error ** errp)94aee7f3ecSGreg Kurz static int validate_opt(void *opaque, const char *name, const char *value,
95aee7f3ecSGreg Kurz Error **errp)
96aee7f3ecSGreg Kurz {
97aee7f3ecSGreg Kurz FsDriverTable *drv = opaque;
98aee7f3ecSGreg Kurz const char **opt;
99aee7f3ecSGreg Kurz
100aee7f3ecSGreg Kurz for (opt = drv->opts; *opt; opt++) {
101aee7f3ecSGreg Kurz if (!strcmp(*opt, name)) {
102aee7f3ecSGreg Kurz return 0;
103aee7f3ecSGreg Kurz }
104aee7f3ecSGreg Kurz }
105aee7f3ecSGreg Kurz
106aee7f3ecSGreg Kurz error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
107aee7f3ecSGreg Kurz return -1;
108aee7f3ecSGreg Kurz }
109aee7f3ecSGreg Kurz
qemu_fsdev_add(QemuOpts * opts,Error ** errp)110b836723dSMarkus Armbruster int qemu_fsdev_add(QemuOpts *opts, Error **errp)
11174db920cSGautham R Shenoy {
11274db920cSGautham R Shenoy int i;
11399519f0aSAneesh Kumar K.V struct FsDriverListEntry *fsle;
1149f506893SHarsh Prateek Bora const char *fsdev_id = qemu_opts_id(opts);
115fbcbf101SAneesh Kumar K.V const char *fsdriver = qemu_opt_get(opts, "fsdriver");
116d3ab98e6SAneesh Kumar K.V const char *writeout = qemu_opt_get(opts, "writeout");
1172c74c2cbSM. Mohan Kumar bool ro = qemu_opt_get_bool(opts, "readonly", 0);
11874db920cSGautham R Shenoy
1199f506893SHarsh Prateek Bora if (!fsdev_id) {
120b836723dSMarkus Armbruster error_setg(errp, "fsdev: No id specified");
12174db920cSGautham R Shenoy return -1;
12274db920cSGautham R Shenoy }
12374db920cSGautham R Shenoy
124fbcbf101SAneesh Kumar K.V if (fsdriver) {
125fbcbf101SAneesh Kumar K.V for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
126fbcbf101SAneesh Kumar K.V if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
12774db920cSGautham R Shenoy break;
12874db920cSGautham R Shenoy }
12974db920cSGautham R Shenoy }
13074db920cSGautham R Shenoy
131fbcbf101SAneesh Kumar K.V if (i == ARRAY_SIZE(FsDrivers)) {
132b836723dSMarkus Armbruster error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
1339f506893SHarsh Prateek Bora return -1;
1349f506893SHarsh Prateek Bora }
1359f506893SHarsh Prateek Bora } else {
136b836723dSMarkus Armbruster error_setg(errp, "fsdev: No fsdriver specified");
13774db920cSGautham R Shenoy return -1;
13874db920cSGautham R Shenoy }
13974db920cSGautham R Shenoy
140aee7f3ecSGreg Kurz if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
141aee7f3ecSGreg Kurz return -1;
142aee7f3ecSGreg Kurz }
143aee7f3ecSGreg Kurz
14499519f0aSAneesh Kumar K.V fsle = g_malloc0(sizeof(*fsle));
1457267c094SAnthony Liguori fsle->fse.fsdev_id = g_strdup(fsdev_id);
146fbcbf101SAneesh Kumar K.V fsle->fse.ops = FsDrivers[i].ops;
147d3ab98e6SAneesh Kumar K.V if (writeout) {
148d3ab98e6SAneesh Kumar K.V if (!strcmp(writeout, "immediate")) {
149b97400caSAneesh Kumar K.V fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
150d3ab98e6SAneesh Kumar K.V }
151d3ab98e6SAneesh Kumar K.V }
1522c74c2cbSM. Mohan Kumar if (ro) {
1532c74c2cbSM. Mohan Kumar fsle->fse.export_flags |= V9FS_RDONLY;
1542c74c2cbSM. Mohan Kumar } else {
1552c74c2cbSM. Mohan Kumar fsle->fse.export_flags &= ~V9FS_RDONLY;
1562c74c2cbSM. Mohan Kumar }
157b97400caSAneesh Kumar K.V
15899519f0aSAneesh Kumar K.V if (fsle->fse.ops->parse_opts) {
159b836723dSMarkus Armbruster if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
160b58c86e1SStefan Weil g_free(fsle->fse.fsdev_id);
161b58c86e1SStefan Weil g_free(fsle);
162d9b36a6eSM. Mohan Kumar return -1;
163b97400caSAneesh Kumar K.V }
16499519f0aSAneesh Kumar K.V }
16599519f0aSAneesh Kumar K.V
166fbcbf101SAneesh Kumar K.V QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
16774db920cSGautham R Shenoy return 0;
16874db920cSGautham R Shenoy }
16974db920cSGautham R Shenoy
get_fsdev_fsentry(char * id)170fbcbf101SAneesh Kumar K.V FsDriverEntry *get_fsdev_fsentry(char *id)
17174db920cSGautham R Shenoy {
1729f506893SHarsh Prateek Bora if (id) {
173fbcbf101SAneesh Kumar K.V struct FsDriverListEntry *fsle;
17474db920cSGautham R Shenoy
175fbcbf101SAneesh Kumar K.V QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
17674db920cSGautham R Shenoy if (strcmp(fsle->fse.fsdev_id, id) == 0) {
17774db920cSGautham R Shenoy return &fsle->fse;
17874db920cSGautham R Shenoy }
17974db920cSGautham R Shenoy }
1809f506893SHarsh Prateek Bora }
18174db920cSGautham R Shenoy return NULL;
18274db920cSGautham R Shenoy }
183