spapr: Fix failure path for attempting to hot unplug PCI bridges
[qemu/ar7.git] / tools / virtiofsd / helper.c
blob819c2bc13cf7c23c2876f175104bd0a5e8f72d12
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 * Helper functions to create (simple) standalone programs. With the
6 * aid of these functions it should be possible to create full FUSE
7 * file system by implementing nothing but the request handlers.
9 * This program can be distributed under the terms of the GNU LGPLv2.
10 * See the file COPYING.LIB.
13 #include "qemu/osdep.h"
14 #include "fuse_i.h"
15 #include "fuse_lowlevel.h"
16 #include "fuse_misc.h"
17 #include "fuse_opt.h"
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/param.h>
26 #include <unistd.h>
28 #define FUSE_HELPER_OPT(t, p) \
29 { \
30 t, offsetof(struct fuse_cmdline_opts, p), 1 \
32 #define FUSE_HELPER_OPT_VALUE(t, p, v) \
33 { \
34 t, offsetof(struct fuse_cmdline_opts, p), v \
37 static const struct fuse_opt fuse_helper_opts[] = {
38 FUSE_HELPER_OPT("-h", show_help),
39 FUSE_HELPER_OPT("--help", show_help),
40 FUSE_HELPER_OPT("-V", show_version),
41 FUSE_HELPER_OPT("--version", show_version),
42 FUSE_HELPER_OPT("--print-capabilities", print_capabilities),
43 FUSE_HELPER_OPT("-d", debug),
44 FUSE_HELPER_OPT("debug", debug),
45 FUSE_HELPER_OPT("-d", foreground),
46 FUSE_HELPER_OPT("debug", foreground),
47 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
48 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
49 FUSE_HELPER_OPT("-f", foreground),
50 FUSE_HELPER_OPT_VALUE("--daemonize", foreground, 0),
51 FUSE_HELPER_OPT("fsname=", nodefault_subtype),
52 FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
53 FUSE_HELPER_OPT("subtype=", nodefault_subtype),
54 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
55 FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
56 FUSE_HELPER_OPT("--syslog", syslog),
57 FUSE_HELPER_OPT_VALUE("log_level=debug", log_level, FUSE_LOG_DEBUG),
58 FUSE_HELPER_OPT_VALUE("log_level=info", log_level, FUSE_LOG_INFO),
59 FUSE_HELPER_OPT_VALUE("log_level=warn", log_level, FUSE_LOG_WARNING),
60 FUSE_HELPER_OPT_VALUE("log_level=err", log_level, FUSE_LOG_ERR),
61 FUSE_OPT_END
64 struct fuse_conn_info_opts {
65 int atomic_o_trunc;
66 int no_remote_posix_lock;
67 int no_remote_flock;
68 int splice_write;
69 int splice_move;
70 int splice_read;
71 int no_splice_write;
72 int no_splice_move;
73 int no_splice_read;
74 int auto_inval_data;
75 int no_auto_inval_data;
76 int no_readdirplus;
77 int no_readdirplus_auto;
78 int async_dio;
79 int no_async_dio;
80 int writeback_cache;
81 int no_writeback_cache;
82 int async_read;
83 int sync_read;
84 unsigned max_write;
85 unsigned max_readahead;
86 unsigned max_background;
87 unsigned congestion_threshold;
88 unsigned time_gran;
89 int set_max_write;
90 int set_max_readahead;
91 int set_max_background;
92 int set_congestion_threshold;
93 int set_time_gran;
96 #define CONN_OPTION(t, p, v) \
97 { \
98 t, offsetof(struct fuse_conn_info_opts, p), v \
100 static const struct fuse_opt conn_info_opt_spec[] = {
101 CONN_OPTION("max_write=%u", max_write, 0),
102 CONN_OPTION("max_write=", set_max_write, 1),
103 CONN_OPTION("max_readahead=%u", max_readahead, 0),
104 CONN_OPTION("max_readahead=", set_max_readahead, 1),
105 CONN_OPTION("max_background=%u", max_background, 0),
106 CONN_OPTION("max_background=", set_max_background, 1),
107 CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
108 CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
109 CONN_OPTION("sync_read", sync_read, 1),
110 CONN_OPTION("async_read", async_read, 1),
111 CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
112 CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
113 CONN_OPTION("no_remote_lock", no_remote_flock, 1),
114 CONN_OPTION("no_remote_flock", no_remote_flock, 1),
115 CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
116 CONN_OPTION("splice_write", splice_write, 1),
117 CONN_OPTION("no_splice_write", no_splice_write, 1),
118 CONN_OPTION("splice_move", splice_move, 1),
119 CONN_OPTION("no_splice_move", no_splice_move, 1),
120 CONN_OPTION("splice_read", splice_read, 1),
121 CONN_OPTION("no_splice_read", no_splice_read, 1),
122 CONN_OPTION("auto_inval_data", auto_inval_data, 1),
123 CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
124 CONN_OPTION("readdirplus=no", no_readdirplus, 1),
125 CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
126 CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
127 CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
128 CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
129 CONN_OPTION("async_dio", async_dio, 1),
130 CONN_OPTION("no_async_dio", no_async_dio, 1),
131 CONN_OPTION("writeback_cache", writeback_cache, 1),
132 CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
133 CONN_OPTION("time_gran=%u", time_gran, 0),
134 CONN_OPTION("time_gran=", set_time_gran, 1),
135 FUSE_OPT_END
139 void fuse_cmdline_help(void)
141 printf(" -h --help print help\n"
142 " -V --version print version\n"
143 " --print-capabilities print vhost-user.json\n"
144 " -d -o debug enable debug output (implies -f)\n"
145 " --syslog log to syslog (default stderr)\n"
146 " -f foreground operation\n"
147 " --daemonize run in background\n"
148 " -o cache=<mode> cache mode. could be one of \"auto, "
149 "always, none\"\n"
150 " default: auto\n"
151 " -o flock|no_flock enable/disable flock\n"
152 " default: no_flock\n"
153 " -o log_level=<level> log level, default to \"info\"\n"
154 " level could be one of \"debug, "
155 "info, warn, err\"\n"
156 " -o max_idle_threads the maximum number of idle worker "
157 "threads\n"
158 " allowed (default: 10)\n"
159 " -o norace disable racy fallback\n"
160 " default: false\n"
161 " -o posix_lock|no_posix_lock\n"
162 " enable/disable remote posix lock\n"
163 " default: posix_lock\n"
164 " -o readdirplus|no_readdirplus\n"
165 " enable/disable readirplus\n"
166 " default: readdirplus except with "
167 "cache=none\n"
168 " -o timeout=<number> I/O timeout (seconds)\n"
169 " default: depends on cache= option.\n"
170 " -o writeback|no_writeback enable/disable writeback cache\n"
171 " default: no_writeback\n"
172 " -o xattr|no_xattr enable/disable xattr\n"
173 " default: no_xattr\n"
177 static int fuse_helper_opt_proc(void *data, const char *arg, int key,
178 struct fuse_args *outargs)
180 (void)data;
181 (void)outargs;
183 switch (key) {
184 case FUSE_OPT_KEY_NONOPT:
185 fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
186 return -1;
188 default:
189 /* Pass through unknown options */
190 return 1;
194 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
196 memset(opts, 0, sizeof(struct fuse_cmdline_opts));
198 opts->max_idle_threads = 10;
199 opts->foreground = 1;
201 if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc) ==
202 -1) {
203 return -1;
206 return 0;
210 int fuse_daemonize(int foreground)
212 int ret = 0, rett;
213 if (!foreground) {
214 int nullfd;
215 int waiter[2];
216 char completed;
218 if (pipe(waiter)) {
219 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: pipe: %s\n",
220 strerror(errno));
221 return -1;
225 * demonize current process by forking it and killing the
226 * parent. This makes current process as a child of 'init'.
228 switch (fork()) {
229 case -1:
230 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: fork: %s\n",
231 strerror(errno));
232 return -1;
233 case 0:
234 break;
235 default:
236 _exit(read(waiter[0], &completed,
237 sizeof(completed) != sizeof(completed)));
240 if (setsid() == -1) {
241 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: setsid: %s\n",
242 strerror(errno));
243 return -1;
246 ret = chdir("/");
248 nullfd = open("/dev/null", O_RDWR, 0);
249 if (nullfd != -1) {
250 rett = dup2(nullfd, 0);
251 if (!ret) {
252 ret = rett;
254 rett = dup2(nullfd, 1);
255 if (!ret) {
256 ret = rett;
258 rett = dup2(nullfd, 2);
259 if (!ret) {
260 ret = rett;
262 if (nullfd > 2) {
263 close(nullfd);
267 /* Propagate completion of daemon initialization */
268 completed = 1;
269 rett = write(waiter[1], &completed, sizeof(completed));
270 if (!ret) {
271 ret = rett;
273 close(waiter[0]);
274 close(waiter[1]);
275 } else {
276 ret = chdir("/");
278 return ret;
281 void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
282 struct fuse_conn_info *conn)
284 if (opts->set_max_write) {
285 conn->max_write = opts->max_write;
287 if (opts->set_max_background) {
288 conn->max_background = opts->max_background;
290 if (opts->set_congestion_threshold) {
291 conn->congestion_threshold = opts->congestion_threshold;
293 if (opts->set_time_gran) {
294 conn->time_gran = opts->time_gran;
296 if (opts->set_max_readahead) {
297 conn->max_readahead = opts->max_readahead;
300 #define LL_ENABLE(cond, cap) \
301 if (cond) \
302 conn->want |= (cap)
303 #define LL_DISABLE(cond, cap) \
304 if (cond) \
305 conn->want &= ~(cap)
307 LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
308 LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
310 LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
311 LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
313 LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
314 LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
316 LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
317 LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
319 LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
320 LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
322 LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
323 LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
325 LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
326 LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
328 LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
329 LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
331 LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
332 LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
335 struct fuse_conn_info_opts *fuse_parse_conn_info_opts(struct fuse_args *args)
337 struct fuse_conn_info_opts *opts;
339 opts = calloc(1, sizeof(struct fuse_conn_info_opts));
340 if (opts == NULL) {
341 fuse_log(FUSE_LOG_ERR, "calloc failed\n");
342 return NULL;
344 if (fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
345 free(opts);
346 return NULL;
348 return opts;