virtiofsd: Allow addition or removal of capabilities
[qemu.git] / tools / virtiofsd / helper.c
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4 *
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.
8
9 * This program can be distributed under the terms of the GNU LGPLv2.
10 * See the file COPYING.LIB.
11 */
12
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"
18
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 <sys/time.h>
27 #include <sys/resource.h>
28 #include <unistd.h>
29
30 #define FUSE_HELPER_OPT(t, p) \
31 { \
32 t, offsetof(struct fuse_cmdline_opts, p), 1 \
33 }
34 #define FUSE_HELPER_OPT_VALUE(t, p, v) \
35 { \
36 t, offsetof(struct fuse_cmdline_opts, p), v \
37 }
38
39 static const struct fuse_opt fuse_helper_opts[] = {
40 FUSE_HELPER_OPT("-h", show_help),
41 FUSE_HELPER_OPT("--help", show_help),
42 FUSE_HELPER_OPT("-V", show_version),
43 FUSE_HELPER_OPT("--version", show_version),
44 FUSE_HELPER_OPT("--print-capabilities", print_capabilities),
45 FUSE_HELPER_OPT("-d", debug),
46 FUSE_HELPER_OPT("debug", debug),
47 FUSE_HELPER_OPT("-d", foreground),
48 FUSE_HELPER_OPT("debug", foreground),
49 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
50 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
51 FUSE_HELPER_OPT("-f", foreground),
52 FUSE_HELPER_OPT_VALUE("--daemonize", foreground, 0),
53 FUSE_HELPER_OPT("fsname=", nodefault_subtype),
54 FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
55 FUSE_HELPER_OPT("subtype=", nodefault_subtype),
56 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
57 FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
58 FUSE_HELPER_OPT("--rlimit-nofile=%lu", rlimit_nofile),
59 FUSE_HELPER_OPT("--syslog", syslog),
60 FUSE_HELPER_OPT_VALUE("log_level=debug", log_level, FUSE_LOG_DEBUG),
61 FUSE_HELPER_OPT_VALUE("log_level=info", log_level, FUSE_LOG_INFO),
62 FUSE_HELPER_OPT_VALUE("log_level=warn", log_level, FUSE_LOG_WARNING),
63 FUSE_HELPER_OPT_VALUE("log_level=err", log_level, FUSE_LOG_ERR),
64 FUSE_OPT_END
65 };
66
67 struct fuse_conn_info_opts {
68 int atomic_o_trunc;
69 int no_remote_posix_lock;
70 int no_remote_flock;
71 int splice_write;
72 int splice_move;
73 int splice_read;
74 int no_splice_write;
75 int no_splice_move;
76 int no_splice_read;
77 int auto_inval_data;
78 int no_auto_inval_data;
79 int no_readdirplus;
80 int no_readdirplus_auto;
81 int async_dio;
82 int no_async_dio;
83 int writeback_cache;
84 int no_writeback_cache;
85 int async_read;
86 int sync_read;
87 unsigned max_write;
88 unsigned max_readahead;
89 unsigned max_background;
90 unsigned congestion_threshold;
91 unsigned time_gran;
92 int set_max_write;
93 int set_max_readahead;
94 int set_max_background;
95 int set_congestion_threshold;
96 int set_time_gran;
97 };
98
99 #define CONN_OPTION(t, p, v) \
100 { \
101 t, offsetof(struct fuse_conn_info_opts, p), v \
102 }
103 static const struct fuse_opt conn_info_opt_spec[] = {
104 CONN_OPTION("max_write=%u", max_write, 0),
105 CONN_OPTION("max_write=", set_max_write, 1),
106 CONN_OPTION("max_readahead=%u", max_readahead, 0),
107 CONN_OPTION("max_readahead=", set_max_readahead, 1),
108 CONN_OPTION("max_background=%u", max_background, 0),
109 CONN_OPTION("max_background=", set_max_background, 1),
110 CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
111 CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
112 CONN_OPTION("sync_read", sync_read, 1),
113 CONN_OPTION("async_read", async_read, 1),
114 CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
115 CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
116 CONN_OPTION("no_remote_lock", no_remote_flock, 1),
117 CONN_OPTION("no_remote_flock", no_remote_flock, 1),
118 CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
119 CONN_OPTION("splice_write", splice_write, 1),
120 CONN_OPTION("no_splice_write", no_splice_write, 1),
121 CONN_OPTION("splice_move", splice_move, 1),
122 CONN_OPTION("no_splice_move", no_splice_move, 1),
123 CONN_OPTION("splice_read", splice_read, 1),
124 CONN_OPTION("no_splice_read", no_splice_read, 1),
125 CONN_OPTION("auto_inval_data", auto_inval_data, 1),
126 CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
127 CONN_OPTION("readdirplus=no", no_readdirplus, 1),
128 CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
129 CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
130 CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
131 CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
132 CONN_OPTION("async_dio", async_dio, 1),
133 CONN_OPTION("no_async_dio", no_async_dio, 1),
134 CONN_OPTION("writeback_cache", writeback_cache, 1),
135 CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
136 CONN_OPTION("time_gran=%u", time_gran, 0),
137 CONN_OPTION("time_gran=", set_time_gran, 1),
138 FUSE_OPT_END
139 };
140
141
142 void fuse_cmdline_help(void)
143 {
144 printf(" -h --help print help\n"
145 " -V --version print version\n"
146 " --print-capabilities print vhost-user.json\n"
147 " -d -o debug enable debug output (implies -f)\n"
148 " --syslog log to syslog (default stderr)\n"
149 " -f foreground operation\n"
150 " --daemonize run in background\n"
151 " -o cache=<mode> cache mode. could be one of \"auto, "
152 "always, none\"\n"
153 " default: auto\n"
154 " -o flock|no_flock enable/disable flock\n"
155 " default: no_flock\n"
156 " -o log_level=<level> log level, default to \"info\"\n"
157 " level could be one of \"debug, "
158 "info, warn, err\"\n"
159 " -o max_idle_threads the maximum number of idle worker "
160 "threads\n"
161 " allowed (default: 10)\n"
162 " -o norace disable racy fallback\n"
163 " default: false\n"
164 " -o posix_lock|no_posix_lock\n"
165 " enable/disable remote posix lock\n"
166 " default: posix_lock\n"
167 " -o readdirplus|no_readdirplus\n"
168 " enable/disable readirplus\n"
169 " default: readdirplus except with "
170 "cache=none\n"
171 " -o timeout=<number> I/O timeout (seconds)\n"
172 " default: depends on cache= option.\n"
173 " -o writeback|no_writeback enable/disable writeback cache\n"
174 " default: no_writeback\n"
175 " -o xattr|no_xattr enable/disable xattr\n"
176 " default: no_xattr\n"
177 " -o modcaps=CAPLIST Modify the list of capabilities\n"
178 " e.g. -o modcaps=+sys_admin:-chown\n"
179 " --rlimit-nofile=<num> set maximum number of file descriptors\n"
180 " (0 leaves rlimit unchanged)\n"
181 " default: min(1000000, fs.file-max - 16384)\n"
182 " if the current rlimit is lower\n"
183 );
184 }
185
186 static int fuse_helper_opt_proc(void *data, const char *arg, int key,
187 struct fuse_args *outargs)
188 {
189 (void)data;
190 (void)outargs;
191
192 switch (key) {
193 case FUSE_OPT_KEY_NONOPT:
194 fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
195 return -1;
196
197 default:
198 /* Pass through unknown options */
199 return 1;
200 }
201 }
202
203 static unsigned long get_default_rlimit_nofile(void)
204 {
205 g_autofree gchar *file_max_str = NULL;
206 const rlim_t reserved_fds = 16384; /* leave at least this many fds free */
207 rlim_t max_fds = 1000000; /* our default RLIMIT_NOFILE target */
208 rlim_t file_max;
209 struct rlimit rlim;
210
211 /*
212 * Reduce max_fds below the system-wide maximum, if necessary. This
213 * ensures there are fds available for other processes so we don't
214 * cause resource exhaustion.
215 */
216 if (!g_file_get_contents("/proc/sys/fs/file-max", &file_max_str,
217 NULL, NULL)) {
218 fuse_log(FUSE_LOG_ERR, "can't read /proc/sys/fs/file-max\n");
219 exit(1);
220 }
221 file_max = g_ascii_strtoull(file_max_str, NULL, 10);
222 if (file_max < 2 * reserved_fds) {
223 fuse_log(FUSE_LOG_ERR,
224 "The fs.file-max sysctl is too low (%lu) to allow a "
225 "reasonable number of open files.\n",
226 (unsigned long)file_max);
227 exit(1);
228 }
229 max_fds = MIN(file_max - reserved_fds, max_fds);
230
231 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
232 fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n");
233 exit(1);
234 }
235
236 if (rlim.rlim_cur >= max_fds) {
237 return 0; /* we have more fds available than required! */
238 }
239 return max_fds;
240 }
241
242 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
243 {
244 memset(opts, 0, sizeof(struct fuse_cmdline_opts));
245
246 opts->max_idle_threads = 10;
247 opts->rlimit_nofile = get_default_rlimit_nofile();
248 opts->foreground = 1;
249
250 if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc) ==
251 -1) {
252 return -1;
253 }
254
255 return 0;
256 }
257
258
259 int fuse_daemonize(int foreground)
260 {
261 int ret = 0, rett;
262 if (!foreground) {
263 int nullfd;
264 int waiter[2];
265 char completed;
266
267 if (pipe(waiter)) {
268 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: pipe: %s\n",
269 strerror(errno));
270 return -1;
271 }
272
273 /*
274 * demonize current process by forking it and killing the
275 * parent. This makes current process as a child of 'init'.
276 */
277 switch (fork()) {
278 case -1:
279 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: fork: %s\n",
280 strerror(errno));
281 return -1;
282 case 0:
283 break;
284 default:
285 _exit(read(waiter[0], &completed,
286 sizeof(completed) != sizeof(completed)));
287 }
288
289 if (setsid() == -1) {
290 fuse_log(FUSE_LOG_ERR, "fuse_daemonize: setsid: %s\n",
291 strerror(errno));
292 return -1;
293 }
294
295 ret = chdir("/");
296
297 nullfd = open("/dev/null", O_RDWR, 0);
298 if (nullfd != -1) {
299 rett = dup2(nullfd, 0);
300 if (!ret) {
301 ret = rett;
302 }
303 rett = dup2(nullfd, 1);
304 if (!ret) {
305 ret = rett;
306 }
307 rett = dup2(nullfd, 2);
308 if (!ret) {
309 ret = rett;
310 }
311 if (nullfd > 2) {
312 close(nullfd);
313 }
314 }
315
316 /* Propagate completion of daemon initialization */
317 completed = 1;
318 rett = write(waiter[1], &completed, sizeof(completed));
319 if (!ret) {
320 ret = rett;
321 }
322 close(waiter[0]);
323 close(waiter[1]);
324 } else {
325 ret = chdir("/");
326 }
327 return ret;
328 }
329
330 void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
331 struct fuse_conn_info *conn)
332 {
333 if (opts->set_max_write) {
334 conn->max_write = opts->max_write;
335 }
336 if (opts->set_max_background) {
337 conn->max_background = opts->max_background;
338 }
339 if (opts->set_congestion_threshold) {
340 conn->congestion_threshold = opts->congestion_threshold;
341 }
342 if (opts->set_time_gran) {
343 conn->time_gran = opts->time_gran;
344 }
345 if (opts->set_max_readahead) {
346 conn->max_readahead = opts->max_readahead;
347 }
348
349 #define LL_ENABLE(cond, cap) \
350 if (cond) \
351 conn->want |= (cap)
352 #define LL_DISABLE(cond, cap) \
353 if (cond) \
354 conn->want &= ~(cap)
355
356 LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
357 LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
358
359 LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
360 LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
361
362 LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
363 LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
364
365 LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
366 LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
367
368 LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
369 LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
370
371 LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
372 LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
373
374 LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
375 LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
376
377 LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
378 LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
379
380 LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
381 LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
382 }
383
384 struct fuse_conn_info_opts *fuse_parse_conn_info_opts(struct fuse_args *args)
385 {
386 struct fuse_conn_info_opts *opts;
387
388 opts = calloc(1, sizeof(struct fuse_conn_info_opts));
389 if (opts == NULL) {
390 fuse_log(FUSE_LOG_ERR, "calloc failed\n");
391 return NULL;
392 }
393 if (fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
394 free(opts);
395 return NULL;
396 }
397 return opts;
398 }