xref: /qemu/fsdev/qemu-fsdev.c (revision 71d72ece)
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",
81353b5a91SPrasad 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,
89353b5a91SPrasad J Pandit             NULL
90aee7f3ecSGreg Kurz         },
91aee7f3ecSGreg Kurz     },
92aee7f3ecSGreg Kurz     {
93aee7f3ecSGreg Kurz         .name = "proxy",
94aee7f3ecSGreg Kurz         .ops = &proxy_ops,
95aee7f3ecSGreg Kurz         .opts = (const char * []) {
96aee7f3ecSGreg Kurz             COMMON_FS_DRIVER_OPTIONS,
97aee7f3ecSGreg Kurz             "socket",
98aee7f3ecSGreg Kurz             "sock_fd",
99aee7f3ecSGreg Kurz             "writeout",
100353b5a91SPrasad J Pandit             NULL
101aee7f3ecSGreg Kurz         },
102aee7f3ecSGreg Kurz     },
10374db920cSGautham R Shenoy };
10474db920cSGautham R Shenoy 
validate_opt(void * opaque,const char * name,const char * value,Error ** errp)105aee7f3ecSGreg Kurz static int validate_opt(void *opaque, const char *name, const char *value,
106aee7f3ecSGreg Kurz                         Error **errp)
107aee7f3ecSGreg Kurz {
108aee7f3ecSGreg Kurz     FsDriverTable *drv = opaque;
109aee7f3ecSGreg Kurz     const char **opt;
110aee7f3ecSGreg Kurz 
111aee7f3ecSGreg Kurz     for (opt = drv->opts; *opt; opt++) {
112aee7f3ecSGreg Kurz         if (!strcmp(*opt, name)) {
113aee7f3ecSGreg Kurz             return 0;
114aee7f3ecSGreg Kurz         }
115aee7f3ecSGreg Kurz     }
116aee7f3ecSGreg Kurz 
117aee7f3ecSGreg Kurz     error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
118aee7f3ecSGreg Kurz     return -1;
119aee7f3ecSGreg Kurz }
120aee7f3ecSGreg Kurz 
qemu_fsdev_add(QemuOpts * opts,Error ** errp)121b836723dSMarkus Armbruster int qemu_fsdev_add(QemuOpts *opts, Error **errp)
12274db920cSGautham R Shenoy {
12374db920cSGautham R Shenoy     int i;
12499519f0aSAneesh Kumar K.V     struct FsDriverListEntry *fsle;
1259f506893SHarsh Prateek Bora     const char *fsdev_id = qemu_opts_id(opts);
126fbcbf101SAneesh Kumar K.V     const char *fsdriver = qemu_opt_get(opts, "fsdriver");
127d3ab98e6SAneesh Kumar K.V     const char *writeout = qemu_opt_get(opts, "writeout");
1282c74c2cbSM. Mohan Kumar     bool ro = qemu_opt_get_bool(opts, "readonly", 0);
12974db920cSGautham R Shenoy 
1309f506893SHarsh Prateek Bora     if (!fsdev_id) {
131b836723dSMarkus Armbruster         error_setg(errp, "fsdev: No id specified");
13274db920cSGautham R Shenoy         return -1;
13374db920cSGautham R Shenoy     }
13474db920cSGautham R Shenoy 
135fbcbf101SAneesh Kumar K.V     if (fsdriver) {
13671d72eceSChristian Schoenebeck         if (strncmp(fsdriver, "proxy", 5) == 0) {
13771d72eceSChristian Schoenebeck             warn_report(
13871d72eceSChristian Schoenebeck                 "'-fsdev proxy' and '-virtfs proxy' are deprecated, use "
13971d72eceSChristian Schoenebeck                 "'local' instead of 'proxy, or consider deploying virtiofsd "
14071d72eceSChristian Schoenebeck                 "as alternative to 9p"
14171d72eceSChristian Schoenebeck             );
14271d72eceSChristian Schoenebeck         }
14371d72eceSChristian Schoenebeck 
144fbcbf101SAneesh Kumar K.V         for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
145fbcbf101SAneesh Kumar K.V             if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
14674db920cSGautham R Shenoy                 break;
14774db920cSGautham R Shenoy             }
14874db920cSGautham R Shenoy         }
14974db920cSGautham R Shenoy 
150fbcbf101SAneesh Kumar K.V         if (i == ARRAY_SIZE(FsDrivers)) {
151b836723dSMarkus Armbruster             error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
1529f506893SHarsh Prateek Bora             return -1;
1539f506893SHarsh Prateek Bora         }
1549f506893SHarsh Prateek Bora     } else {
155b836723dSMarkus Armbruster         error_setg(errp, "fsdev: No fsdriver specified");
15674db920cSGautham R Shenoy         return -1;
15774db920cSGautham R Shenoy     }
15874db920cSGautham R Shenoy 
159aee7f3ecSGreg Kurz     if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
160aee7f3ecSGreg Kurz         return -1;
161aee7f3ecSGreg Kurz     }
162aee7f3ecSGreg Kurz 
16399519f0aSAneesh Kumar K.V     fsle = g_malloc0(sizeof(*fsle));
1647267c094SAnthony Liguori     fsle->fse.fsdev_id = g_strdup(fsdev_id);
165fbcbf101SAneesh Kumar K.V     fsle->fse.ops = FsDrivers[i].ops;
166d3ab98e6SAneesh Kumar K.V     if (writeout) {
167d3ab98e6SAneesh Kumar K.V         if (!strcmp(writeout, "immediate")) {
168b97400caSAneesh Kumar K.V             fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
169d3ab98e6SAneesh Kumar K.V         }
170d3ab98e6SAneesh Kumar K.V     }
1712c74c2cbSM. Mohan Kumar     if (ro) {
1722c74c2cbSM. Mohan Kumar         fsle->fse.export_flags |= V9FS_RDONLY;
1732c74c2cbSM. Mohan Kumar     } else {
1742c74c2cbSM. Mohan Kumar         fsle->fse.export_flags &= ~V9FS_RDONLY;
1752c74c2cbSM. Mohan Kumar     }
176b97400caSAneesh Kumar K.V 
17799519f0aSAneesh Kumar K.V     if (fsle->fse.ops->parse_opts) {
178b836723dSMarkus Armbruster         if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
179b58c86e1SStefan Weil             g_free(fsle->fse.fsdev_id);
180b58c86e1SStefan Weil             g_free(fsle);
181d9b36a6eSM. Mohan Kumar             return -1;
182b97400caSAneesh Kumar K.V         }
18399519f0aSAneesh Kumar K.V     }
18499519f0aSAneesh Kumar K.V 
185fbcbf101SAneesh Kumar K.V     QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
18674db920cSGautham R Shenoy     return 0;
18774db920cSGautham R Shenoy }
18874db920cSGautham R Shenoy 
get_fsdev_fsentry(char * id)189fbcbf101SAneesh Kumar K.V FsDriverEntry *get_fsdev_fsentry(char *id)
19074db920cSGautham R Shenoy {
1919f506893SHarsh Prateek Bora     if (id) {
192fbcbf101SAneesh Kumar K.V         struct FsDriverListEntry *fsle;
19374db920cSGautham R Shenoy 
194fbcbf101SAneesh Kumar K.V         QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
19574db920cSGautham R Shenoy             if (strcmp(fsle->fse.fsdev_id, id) == 0) {
19674db920cSGautham R Shenoy                 return &fsle->fse;
19774db920cSGautham R Shenoy             }
19874db920cSGautham R Shenoy         }
1999f506893SHarsh Prateek Bora     }
20074db920cSGautham R Shenoy     return NULL;
20174db920cSGautham R Shenoy }
202