xref: /openbmc/qemu/fsdev/qemu-fsdev.c (revision ae3c12a0)
1 /*
2  * 9p
3  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-fsdev.h"
16 #include "qemu/queue.h"
17 #include "qemu/config-file.h"
18 #include "qemu/error-report.h"
19 #include "qemu/option.h"
20 
21 /*
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  */
34 typedef struct FsDriverTable {
35     const char *name;
36     FileOperations *ops;
37     const char **opts;
38 } FsDriverTable;
39 
40 typedef struct FsDriverListEntry {
41     FsDriverEntry fse;
42     QTAILQ_ENTRY(FsDriverListEntry) next;
43 } FsDriverListEntry;
44 
45 static QTAILQ_HEAD(, FsDriverListEntry) fsdriver_entries =
46     QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
47 
48 #define COMMON_FS_DRIVER_OPTIONS "id", "fsdriver", "readonly"
49 
50 static FsDriverTable FsDrivers[] = {
51     {
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",
61             "throttling.bps-total",
62             "throttling.bps-read",
63             "throttling.bps-write",
64             "throttling.iops-total",
65             "throttling.iops-read",
66             "throttling.iops-write",
67             "throttling.bps-total-max",
68             "throttling.bps-read-max",
69             "throttling.bps-write-max",
70             "throttling.iops-total-max",
71             "throttling.iops-read-max",
72             "throttling.iops-write-max",
73             "throttling.bps-total-max-length",
74             "throttling.bps-read-max-length",
75             "throttling.bps-write-max-length",
76             "throttling.iops-total-max-length",
77             "throttling.iops-read-max-length",
78             "throttling.iops-write-max-length",
79             "throttling.iops-size",
80         },
81     },
82     {
83         .name = "synth",
84         .ops = &synth_ops,
85         .opts = (const char * []) {
86             COMMON_FS_DRIVER_OPTIONS,
87         },
88     },
89     {
90         .name = "proxy",
91         .ops = &proxy_ops,
92         .opts = (const char * []) {
93             COMMON_FS_DRIVER_OPTIONS,
94             "socket",
95             "sock_fd",
96             "writeout",
97         },
98     },
99 };
100 
101 static int validate_opt(void *opaque, const char *name, const char *value,
102                         Error **errp)
103 {
104     FsDriverTable *drv = opaque;
105     const char **opt;
106 
107     for (opt = drv->opts; *opt; opt++) {
108         if (!strcmp(*opt, name)) {
109             return 0;
110         }
111     }
112 
113     error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
114     return -1;
115 }
116 
117 int qemu_fsdev_add(QemuOpts *opts, Error **errp)
118 {
119     int i;
120     struct FsDriverListEntry *fsle;
121     const char *fsdev_id = qemu_opts_id(opts);
122     const char *fsdriver = qemu_opt_get(opts, "fsdriver");
123     const char *writeout = qemu_opt_get(opts, "writeout");
124     bool ro = qemu_opt_get_bool(opts, "readonly", 0);
125 
126     if (!fsdev_id) {
127         error_setg(errp, "fsdev: No id specified");
128         return -1;
129     }
130 
131     if (fsdriver) {
132         for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
133             if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
134                 break;
135             }
136         }
137 
138         if (i == ARRAY_SIZE(FsDrivers)) {
139             error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
140             return -1;
141         }
142     } else {
143         error_setg(errp, "fsdev: No fsdriver specified");
144         return -1;
145     }
146 
147     if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
148         return -1;
149     }
150 
151     fsle = g_malloc0(sizeof(*fsle));
152     fsle->fse.fsdev_id = g_strdup(fsdev_id);
153     fsle->fse.ops = FsDrivers[i].ops;
154     if (writeout) {
155         if (!strcmp(writeout, "immediate")) {
156             fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
157         }
158     }
159     if (ro) {
160         fsle->fse.export_flags |= V9FS_RDONLY;
161     } else {
162         fsle->fse.export_flags &= ~V9FS_RDONLY;
163     }
164 
165     if (fsle->fse.ops->parse_opts) {
166         if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
167             g_free(fsle->fse.fsdev_id);
168             g_free(fsle);
169             return -1;
170         }
171     }
172 
173     QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
174     return 0;
175 }
176 
177 FsDriverEntry *get_fsdev_fsentry(char *id)
178 {
179     if (id) {
180         struct FsDriverListEntry *fsle;
181 
182         QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
183             if (strcmp(fsle->fse.fsdev_id, id) == 0) {
184                 return &fsle->fse;
185             }
186         }
187     }
188     return NULL;
189 }
190