virtiofsd: Add options for virtio
[qemu/kevin.git] / tools / virtiofsd / helper.c
blob676032e71f4cae49f4cb1e24d070141eb515dfeb
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 \
33 static const struct fuse_opt fuse_helper_opts[] = {
34 FUSE_HELPER_OPT("-h", show_help),
35 FUSE_HELPER_OPT("--help", show_help),
36 FUSE_HELPER_OPT("-V", show_version),
37 FUSE_HELPER_OPT("--version", show_version),
38 FUSE_HELPER_OPT("-d", debug),
39 FUSE_HELPER_OPT("debug", debug),
40 FUSE_HELPER_OPT("-d", foreground),
41 FUSE_HELPER_OPT("debug", foreground),
42 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
43 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
44 FUSE_HELPER_OPT("-f", foreground),
45 FUSE_HELPER_OPT("fsname=", nodefault_subtype),
46 FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
47 FUSE_HELPER_OPT("subtype=", nodefault_subtype),
48 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
49 FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
50 FUSE_OPT_END
53 struct fuse_conn_info_opts {
54 int atomic_o_trunc;
55 int no_remote_posix_lock;
56 int no_remote_flock;
57 int splice_write;
58 int splice_move;
59 int splice_read;
60 int no_splice_write;
61 int no_splice_move;
62 int no_splice_read;
63 int auto_inval_data;
64 int no_auto_inval_data;
65 int no_readdirplus;
66 int no_readdirplus_auto;
67 int async_dio;
68 int no_async_dio;
69 int writeback_cache;
70 int no_writeback_cache;
71 int async_read;
72 int sync_read;
73 unsigned max_write;
74 unsigned max_readahead;
75 unsigned max_background;
76 unsigned congestion_threshold;
77 unsigned time_gran;
78 int set_max_write;
79 int set_max_readahead;
80 int set_max_background;
81 int set_congestion_threshold;
82 int set_time_gran;
85 #define CONN_OPTION(t, p, v) \
86 { \
87 t, offsetof(struct fuse_conn_info_opts, p), v \
89 static const struct fuse_opt conn_info_opt_spec[] = {
90 CONN_OPTION("max_write=%u", max_write, 0),
91 CONN_OPTION("max_write=", set_max_write, 1),
92 CONN_OPTION("max_readahead=%u", max_readahead, 0),
93 CONN_OPTION("max_readahead=", set_max_readahead, 1),
94 CONN_OPTION("max_background=%u", max_background, 0),
95 CONN_OPTION("max_background=", set_max_background, 1),
96 CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
97 CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
98 CONN_OPTION("sync_read", sync_read, 1),
99 CONN_OPTION("async_read", async_read, 1),
100 CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
101 CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
102 CONN_OPTION("no_remote_lock", no_remote_flock, 1),
103 CONN_OPTION("no_remote_flock", no_remote_flock, 1),
104 CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
105 CONN_OPTION("splice_write", splice_write, 1),
106 CONN_OPTION("no_splice_write", no_splice_write, 1),
107 CONN_OPTION("splice_move", splice_move, 1),
108 CONN_OPTION("no_splice_move", no_splice_move, 1),
109 CONN_OPTION("splice_read", splice_read, 1),
110 CONN_OPTION("no_splice_read", no_splice_read, 1),
111 CONN_OPTION("auto_inval_data", auto_inval_data, 1),
112 CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
113 CONN_OPTION("readdirplus=no", no_readdirplus, 1),
114 CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
115 CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
116 CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
117 CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
118 CONN_OPTION("async_dio", async_dio, 1),
119 CONN_OPTION("no_async_dio", no_async_dio, 1),
120 CONN_OPTION("writeback_cache", writeback_cache, 1),
121 CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
122 CONN_OPTION("time_gran=%u", time_gran, 0),
123 CONN_OPTION("time_gran=", set_time_gran, 1),
124 FUSE_OPT_END
128 void fuse_cmdline_help(void)
130 printf(" -h --help print help\n"
131 " -V --version print version\n"
132 " -d -o debug enable debug output (implies -f)\n"
133 " -f foreground operation\n"
134 " -o max_idle_threads the maximum number of idle worker "
135 "threads\n"
136 " allowed (default: 10)\n");
139 static int fuse_helper_opt_proc(void *data, const char *arg, int key,
140 struct fuse_args *outargs)
142 (void)data;
143 (void)outargs;
145 switch (key) {
146 case FUSE_OPT_KEY_NONOPT:
147 fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
148 return -1;
150 default:
151 /* Pass through unknown options */
152 return 1;
156 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
158 memset(opts, 0, sizeof(struct fuse_cmdline_opts));
160 opts->max_idle_threads = 10;
162 if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc) ==
163 -1) {
164 return -1;
167 return 0;
171 int fuse_daemonize(int foreground)
173 int ret = 0, rett;
174 if (!foreground) {
175 int nullfd;
176 int waiter[2];
177 char completed;
179 if (pipe(waiter)) {
180 perror("fuse_daemonize: pipe");
181 return -1;
185 * demonize current process by forking it and killing the
186 * parent. This makes current process as a child of 'init'.
188 switch (fork()) {
189 case -1:
190 perror("fuse_daemonize: fork");
191 return -1;
192 case 0:
193 break;
194 default:
195 _exit(read(waiter[0], &completed,
196 sizeof(completed) != sizeof(completed)));
199 if (setsid() == -1) {
200 perror("fuse_daemonize: setsid");
201 return -1;
204 ret = chdir("/");
206 nullfd = open("/dev/null", O_RDWR, 0);
207 if (nullfd != -1) {
208 rett = dup2(nullfd, 0);
209 if (!ret) {
210 ret = rett;
212 rett = dup2(nullfd, 1);
213 if (!ret) {
214 ret = rett;
216 rett = dup2(nullfd, 2);
217 if (!ret) {
218 ret = rett;
220 if (nullfd > 2) {
221 close(nullfd);
225 /* Propagate completion of daemon initialization */
226 completed = 1;
227 rett = write(waiter[1], &completed, sizeof(completed));
228 if (!ret) {
229 ret = rett;
231 close(waiter[0]);
232 close(waiter[1]);
233 } else {
234 ret = chdir("/");
236 return ret;
239 void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
240 struct fuse_conn_info *conn)
242 if (opts->set_max_write) {
243 conn->max_write = opts->max_write;
245 if (opts->set_max_background) {
246 conn->max_background = opts->max_background;
248 if (opts->set_congestion_threshold) {
249 conn->congestion_threshold = opts->congestion_threshold;
251 if (opts->set_time_gran) {
252 conn->time_gran = opts->time_gran;
254 if (opts->set_max_readahead) {
255 conn->max_readahead = opts->max_readahead;
258 #define LL_ENABLE(cond, cap) \
259 if (cond) \
260 conn->want |= (cap)
261 #define LL_DISABLE(cond, cap) \
262 if (cond) \
263 conn->want &= ~(cap)
265 LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
266 LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
268 LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
269 LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
271 LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
272 LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
274 LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
275 LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
277 LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
278 LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
280 LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
281 LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
283 LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
284 LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
286 LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
287 LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
289 LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
290 LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
293 struct fuse_conn_info_opts *fuse_parse_conn_info_opts(struct fuse_args *args)
295 struct fuse_conn_info_opts *opts;
297 opts = calloc(1, sizeof(struct fuse_conn_info_opts));
298 if (opts == NULL) {
299 fuse_log(FUSE_LOG_ERR, "calloc failed\n");
300 return NULL;
302 if (fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
303 free(opts);
304 return NULL;
306 return opts;