blob: f5c953a7105e1d42d3a99488f62d70ebac6e9e7c [file] [log] [blame]
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301/*
Greg Kurzaf8b38b2016-06-06 11:52:34 +02002 * 9p
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Gautham R Shenoy <ego@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.
Gautham R Shenoy74db9202010-04-29 17:44:43 +053011 */
Markus Armbrustere688df62018-02-01 12:18:31 +010012
Peter Maydellfbc04122016-01-26 18:17:10 +000013#include "qemu/osdep.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010014#include "qapi/error.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +053015#include "qemu-fsdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/config-file.h"
Greg Kurzea753f32016-01-22 15:12:18 +010018#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010019#include "qemu/option.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +053020
Greg Kurz20232432019-05-17 17:34:48 +020021/*
22 * A table to store the various file systems and their callback operations.
23 * -----------------
24 * fstype | ops
25 * -----------------
26 * local | local_ops
27 * . |
28 * . |
29 * . |
30 * . |
31 * -----------------
32 * etc
33 */
34typedef struct FsDriverTable {
35 const char *name;
36 FileOperations *ops;
Greg Kurzaee7f3e2019-05-17 17:34:48 +020037 const char **opts;
Greg Kurz20232432019-05-17 17:34:48 +020038} FsDriverTable;
39
40typedef struct FsDriverListEntry {
41 FsDriverEntry fse;
42 QTAILQ_ENTRY(FsDriverListEntry) next;
43} FsDriverListEntry;
44
Paolo Bonzinib58deb32018-12-06 11:58:10 +010045static QTAILQ_HEAD(, FsDriverListEntry) fsdriver_entries =
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +053046 QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
Gautham R Shenoy74db9202010-04-29 17:44:43 +053047
Greg Kurzaee7f3e2019-05-17 17:34:48 +020048#define COMMON_FS_DRIVER_OPTIONS "id", "fsdriver", "readonly"
49
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +053050static FsDriverTable FsDrivers[] = {
Greg Kurzaee7f3e2019-05-17 17:34:48 +020051 {
52 .name = "local",
53 .ops = &local_ops,
54 .opts = (const char * []) {
55 COMMON_FS_DRIVER_OPTIONS,
56 "security_model",
57 "path",
58 "writeout",
59 "fmode",
60 "dmode",
Antonios Motakis1a6ed332019-10-10 11:36:05 +020061 "multidevs",
Greg Kurzaee7f3e2019-05-17 17:34:48 +020062 "throttling.bps-total",
63 "throttling.bps-read",
64 "throttling.bps-write",
65 "throttling.iops-total",
66 "throttling.iops-read",
67 "throttling.iops-write",
68 "throttling.bps-total-max",
69 "throttling.bps-read-max",
70 "throttling.bps-write-max",
71 "throttling.iops-total-max",
72 "throttling.iops-read-max",
73 "throttling.iops-write-max",
74 "throttling.bps-total-max-length",
75 "throttling.bps-read-max-length",
76 "throttling.bps-write-max-length",
77 "throttling.iops-total-max-length",
78 "throttling.iops-read-max-length",
79 "throttling.iops-write-max-length",
80 "throttling.iops-size",
Prasad J Pandit353b5a92020-07-09 23:28:48 +053081 NULL
Greg Kurzaee7f3e2019-05-17 17:34:48 +020082 },
83 },
84 {
85 .name = "synth",
86 .ops = &synth_ops,
87 .opts = (const char * []) {
88 COMMON_FS_DRIVER_OPTIONS,
Prasad J Pandit353b5a92020-07-09 23:28:48 +053089 NULL
Greg Kurzaee7f3e2019-05-17 17:34:48 +020090 },
91 },
92 {
93 .name = "proxy",
94 .ops = &proxy_ops,
95 .opts = (const char * []) {
96 COMMON_FS_DRIVER_OPTIONS,
97 "socket",
98 "sock_fd",
99 "writeout",
Prasad J Pandit353b5a92020-07-09 23:28:48 +0530100 NULL
Greg Kurzaee7f3e2019-05-17 17:34:48 +0200101 },
102 },
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530103};
104
Greg Kurzaee7f3e2019-05-17 17:34:48 +0200105static int validate_opt(void *opaque, const char *name, const char *value,
106 Error **errp)
107{
108 FsDriverTable *drv = opaque;
109 const char **opt;
110
111 for (opt = drv->opts; *opt; opt++) {
112 if (!strcmp(*opt, name)) {
113 return 0;
114 }
115 }
116
117 error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
118 return -1;
119}
120
Markus Armbrusterb8367232018-10-17 10:26:55 +0200121int qemu_fsdev_add(QemuOpts *opts, Error **errp)
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530122{
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530123 int i;
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530124 struct FsDriverListEntry *fsle;
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530125 const char *fsdev_id = qemu_opts_id(opts);
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530126 const char *fsdriver = qemu_opt_get(opts, "fsdriver");
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530127 const char *writeout = qemu_opt_get(opts, "writeout");
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +0530128 bool ro = qemu_opt_get_bool(opts, "readonly", 0);
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530129
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530130 if (!fsdev_id) {
Markus Armbrusterb8367232018-10-17 10:26:55 +0200131 error_setg(errp, "fsdev: No id specified");
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530132 return -1;
133 }
134
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530135 if (fsdriver) {
Christian Schoenebeck71d72ec2023-06-26 13:49:06 +0200136 if (strncmp(fsdriver, "proxy", 5) == 0) {
137 warn_report(
138 "'-fsdev proxy' and '-virtfs proxy' are deprecated, use "
139 "'local' instead of 'proxy, or consider deploying virtiofsd "
140 "as alternative to 9p"
141 );
142 }
143
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530144 for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
145 if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530146 break;
147 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530148 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530149
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530150 if (i == ARRAY_SIZE(FsDrivers)) {
Markus Armbrusterb8367232018-10-17 10:26:55 +0200151 error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530152 return -1;
153 }
154 } else {
Markus Armbrusterb8367232018-10-17 10:26:55 +0200155 error_setg(errp, "fsdev: No fsdriver specified");
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530156 return -1;
157 }
158
Greg Kurzaee7f3e2019-05-17 17:34:48 +0200159 if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
160 return -1;
161 }
162
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530163 fsle = g_malloc0(sizeof(*fsle));
Anthony Liguori7267c092011-08-20 22:09:37 -0500164 fsle->fse.fsdev_id = g_strdup(fsdev_id);
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530165 fsle->fse.ops = FsDrivers[i].ops;
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530166 if (writeout) {
167 if (!strcmp(writeout, "immediate")) {
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530168 fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
Aneesh Kumar K.Vd3ab98e2011-10-12 19:11:23 +0530169 }
170 }
M. Mohan Kumar2c74c2c2011-10-25 12:10:39 +0530171 if (ro) {
172 fsle->fse.export_flags |= V9FS_RDONLY;
173 } else {
174 fsle->fse.export_flags &= ~V9FS_RDONLY;
175 }
Aneesh Kumar K.Vb97400c2011-10-13 13:21:00 +0530176
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530177 if (fsle->fse.ops->parse_opts) {
Markus Armbrusterb8367232018-10-17 10:26:55 +0200178 if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
Stefan Weilb58c86e2013-06-16 12:02:40 +0200179 g_free(fsle->fse.fsdev_id);
180 g_free(fsle);
Aneesh Kumar K.V99519f02011-12-14 13:48:59 +0530181 return -1;
182 }
M. Mohan Kumard9b36a62011-10-14 12:59:37 +0530183 }
184
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530185 QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530186 return 0;
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530187}
188
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530189FsDriverEntry *get_fsdev_fsentry(char *id)
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530190{
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530191 if (id) {
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530192 struct FsDriverListEntry *fsle;
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530193
Aneesh Kumar K.Vfbcbf102011-10-13 12:28:04 +0530194 QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
Harsh Prateek Bora9f506892010-10-18 15:36:36 +0530195 if (strcmp(fsle->fse.fsdev_id, id) == 0) {
196 return &fsle->fse;
197 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530198 }
199 }
200 return NULL;
201}