libfuse
fuse_loop_mt.c
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 Implementation of the multi-threaded FUSE session loop.
6
7 This program can be distributed under the terms of the GNU LGPLv2.
8 See the file LGPL2.txt.
9*/
10
11#define _GNU_SOURCE
12
13#include "fuse_config.h"
14#include "fuse_lowlevel.h"
15#include "fuse_misc.h"
16#include "fuse_kernel.h"
17#include "fuse_i.h"
18#include "fuse_uring_i.h"
19#include "util.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <signal.h>
26#include <semaphore.h>
27#include <errno.h>
28#include <sys/time.h>
29#include <sys/ioctl.h>
30#include <assert.h>
31#include <limits.h>
32
33/* Environment var controlling the thread stack size */
34#define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
35
36#define FUSE_LOOP_MT_V2_IDENTIFIER INT_MAX - 2
37#define FUSE_LOOP_MT_DEF_CLONE_FD 0
38#define FUSE_LOOP_MT_DEF_MAX_THREADS 10
39#define FUSE_LOOP_MT_DEF_IDLE_THREADS -1 /* thread destruction is disabled
40 * by default */
41
42/* an arbitrary large value that cannot be valid */
43#define FUSE_LOOP_MT_MAX_THREADS (100U * 1000)
44
45struct fuse_worker {
46 struct fuse_worker *prev;
47 struct fuse_worker *next;
48 pthread_t thread_id;
49
50 // We need to include fuse_buf so that we can properly free
51 // it when a thread is terminated by pthread_cancel().
52 struct fuse_buf fbuf;
53 struct fuse_chan *ch;
54 struct fuse_mt *mt;
55};
56
57/* synchronization via se->mt_lock */
58struct fuse_mt {
59 int numworker;
60 int numavail;
61 struct fuse_session *se;
62 struct fuse_worker main;
63 int error;
64 int clone_fd;
65 int max_idle;
66 int max_threads;
67};
68
69static struct fuse_chan *fuse_chan_new(int fd)
70{
71 struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch));
72 if (ch == NULL) {
73 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate channel\n");
74 return NULL;
75 }
76
77 memset(ch, 0, sizeof(*ch));
78 ch->fd = fd;
79 ch->ctr = 1;
80 pthread_mutex_init(&ch->lock, NULL);
81
82 return ch;
83}
84
85struct fuse_chan *fuse_chan_get(struct fuse_chan *ch)
86{
87 assert(ch->ctr > 0);
88 pthread_mutex_lock(&ch->lock);
89 ch->ctr++;
90 pthread_mutex_unlock(&ch->lock);
91
92 return ch;
93}
94
95void fuse_chan_put(struct fuse_chan *ch)
96{
97 if (ch == NULL)
98 return;
99 pthread_mutex_lock(&ch->lock);
100 ch->ctr--;
101 if (!ch->ctr) {
102 pthread_mutex_unlock(&ch->lock);
103 close(ch->fd);
104 pthread_mutex_destroy(&ch->lock);
105 free(ch);
106 } else
107 pthread_mutex_unlock(&ch->lock);
108}
109
110static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
111{
112 struct fuse_worker *prev = next->prev;
113 w->next = next;
114 w->prev = prev;
115 prev->next = w;
116 next->prev = w;
117}
118
119static void list_del_worker(struct fuse_worker *w)
120{
121 struct fuse_worker *prev = w->prev;
122 struct fuse_worker *next = w->next;
123 prev->next = next;
124 next->prev = prev;
125}
126
127static int fuse_loop_start_thread(struct fuse_mt *mt);
128
129static void *fuse_do_work(void *data)
130{
131 struct fuse_worker *w = (struct fuse_worker *) data;
132 struct fuse_mt *mt = w->mt;
133 struct fuse_session *se = mt->se;
134
135 fuse_set_thread_name("fuse_worker");
136
137 while (!fuse_session_exited(se)) {
138 int isforget = 0;
139 int res;
140
141 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
142 res = fuse_session_receive_buf_internal(se, &w->fbuf, w->ch);
143 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
144 if (res == -EINTR)
145 continue;
146 if (res <= 0) {
147 if (res < 0) {
149 mt->error = res;
150 }
151 break;
152 }
153
154 pthread_mutex_lock(&se->mt_lock);
155 if (fuse_session_exited(se)) {
156 pthread_mutex_unlock(&se->mt_lock);
157 return NULL;
158 }
159
160 /*
161 * This disgusting hack is needed so that zillions of threads
162 * are not created on a burst of FORGET messages
163 */
164 if (!(w->fbuf.flags & FUSE_BUF_IS_FD)) {
165 struct fuse_in_header *in = w->fbuf.mem;
166
167 if (in->opcode == FUSE_FORGET ||
168 in->opcode == FUSE_BATCH_FORGET)
169 isforget = 1;
170 }
171
172 if (!isforget)
173 mt->numavail--;
174 if (mt->numavail == 0 && mt->numworker < mt->max_threads &&
175 likely(se->got_init))
176 fuse_loop_start_thread(mt);
177 pthread_mutex_unlock(&se->mt_lock);
178
179 fuse_session_process_buf_internal(se, &w->fbuf, w->ch);
180
181 pthread_mutex_lock(&se->mt_lock);
182 if (!isforget)
183 mt->numavail++;
184
185 /* creating and destroying threads is rather expensive - and there is
186 * not much gain from destroying existing threads. It is therefore
187 * discouraged to set max_idle to anything else than -1. If there
188 * is indeed a good reason to destruct threads it should be done
189 * delayed, a moving average might be useful for that.
190 */
191 if (mt->max_idle != -1 && mt->numavail > mt->max_idle && mt->numworker > 1) {
192 if (fuse_session_exited(se)) {
193 pthread_mutex_unlock(&se->mt_lock);
194 return NULL;
195 }
196 list_del_worker(w);
197 mt->numavail--;
198 mt->numworker--;
199 pthread_mutex_unlock(&se->mt_lock);
200
201 pthread_detach(w->thread_id);
202 fuse_buf_free(&w->fbuf);
203 fuse_chan_put(w->ch);
204 free(w);
205 return NULL;
206 }
207 pthread_mutex_unlock(&se->mt_lock);
208 }
209
210 sem_post(&se->mt_finish);
211 return NULL;
212}
213
214int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
215{
216 sigset_t oldset;
217 sigset_t newset;
218 int res;
219 pthread_attr_t attr;
220 char *stack_size;
221
222 /* Override default stack size
223 * XXX: This should ideally be a parameter option. It is rather
224 * well hidden here.
225 */
226 pthread_attr_init(&attr);
227 stack_size = getenv(ENVNAME_THREAD_STACK);
228 if (stack_size) {
229 long size;
230
231 res = libfuse_strtol(stack_size, &size);
232 if (res)
233 fuse_log(FUSE_LOG_ERR, "fuse: invalid stack size: %s\n",
234 stack_size);
235 else if (pthread_attr_setstacksize(&attr, size))
236 fuse_log(FUSE_LOG_ERR, "fuse: could not set stack size: %ld\n",
237 size);
238 }
239
240 /* Disallow signal reception in worker threads */
241 sigemptyset(&newset);
242 sigaddset(&newset, SIGTERM);
243 sigaddset(&newset, SIGINT);
244 sigaddset(&newset, SIGHUP);
245 sigaddset(&newset, SIGQUIT);
246 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
247 res = pthread_create(thread_id, &attr, func, arg);
248 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
249 pthread_attr_destroy(&attr);
250 if (res != 0) {
251 fuse_log(FUSE_LOG_ERR, "fuse: error creating thread: %s\n",
252 strerror(res));
253 return -1;
254 }
255
256 return 0;
257}
258
259static int fuse_clone_chan_fd_default(struct fuse_session *se)
260{
261 int res;
262 int clonefd;
263 uint32_t masterfd;
264 const char *devname = "/dev/fuse";
265
266#ifndef O_CLOEXEC
267#define O_CLOEXEC 0
268#endif
269 clonefd = open(devname, O_RDWR | O_CLOEXEC);
270 if (clonefd == -1) {
271 fuse_log(FUSE_LOG_ERR, "fuse: failed to open %s: %s\n", devname,
272 strerror(errno));
273 return -1;
274 }
275 if (!O_CLOEXEC) {
276 res = fcntl(clonefd, F_SETFD, FD_CLOEXEC);
277 if (res == -1) {
278 fuse_log(FUSE_LOG_ERR, "fuse: failed to set CLOEXEC: %s\n",
279 strerror(errno));
280 close(clonefd);
281 return -1;
282 }
283 }
284
285 masterfd = se->fd;
286 res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
287 if (res == -1) {
288 fuse_log(FUSE_LOG_ERR, "fuse: failed to clone device fd: %s\n",
289 strerror(errno));
290 close(clonefd);
291 return -1;
292 }
293 return clonefd;
294}
295
296static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
297{
298 int clonefd;
299 struct fuse_session *se = mt->se;
300 struct fuse_chan *newch;
301
302 if (se->io != NULL) {
303 if (se->io->clone_fd != NULL)
304 clonefd = se->io->clone_fd(se->fd);
305 else
306 return NULL;
307 } else {
308 clonefd = fuse_clone_chan_fd_default(se);
309 }
310 if (clonefd < 0)
311 return NULL;
312
313 newch = fuse_chan_new(clonefd);
314 if (newch == NULL)
315 close(clonefd);
316
317 return newch;
318}
319
320static int fuse_loop_start_thread(struct fuse_mt *mt)
321{
322 int res;
323
324 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
325 if (!w) {
326 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate worker structure\n");
327 return -1;
328 }
329 memset(w, 0, sizeof(struct fuse_worker));
330 w->fbuf.mem = NULL;
331 w->mt = mt;
332
333 w->ch = NULL;
334 if (mt->clone_fd) {
335 w->ch = fuse_clone_chan(mt);
336 if(!w->ch) {
337 /* Don't attempt this again */
338 fuse_log(FUSE_LOG_ERR, "fuse: trying to continue "
339 "without -o clone_fd.\n");
340 mt->clone_fd = 0;
341 }
342 }
343
344 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
345 if (res == -1) {
346 fuse_chan_put(w->ch);
347 free(w);
348 return -1;
349 }
350 list_add_worker(w, &mt->main);
351 mt->numavail ++;
352 mt->numworker ++;
353
354 return 0;
355}
356
357static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
358{
359 pthread_join(w->thread_id, NULL);
360 pthread_mutex_lock(&mt->se->mt_lock);
361 list_del_worker(w);
362 pthread_mutex_unlock(&mt->se->mt_lock);
363 fuse_buf_free(&w->fbuf);
364 fuse_chan_put(w->ch);
365 free(w);
366}
367
368int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config);
369FUSE_SYMVER("fuse_session_loop_mt_312", "fuse_session_loop_mt@@FUSE_3.12")
370int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config)
371{
372int err;
373 struct fuse_mt mt;
374 struct fuse_worker *w;
375 int created_config = 0;
376
377 if (config) {
378 err = fuse_loop_cfg_verify(config);
379 if (err)
380 return err;
381 } else {
382 /* The caller does not care about parameters - use the default */
383 config = fuse_loop_cfg_create();
384 created_config = 1;
385 }
386
387
388 memset(&mt, 0, sizeof(struct fuse_mt));
389 mt.se = se;
390 mt.clone_fd = config->clone_fd;
391 mt.error = 0;
392 mt.numworker = 0;
393 mt.numavail = 0;
394 mt.max_idle = config->max_idle_threads;
395 mt.max_threads = config->max_threads;
396 mt.main.thread_id = pthread_self();
397 mt.main.prev = mt.main.next = &mt.main;
398
399 pthread_mutex_lock(&se->mt_lock);
400 err = fuse_loop_start_thread(&mt);
401 pthread_mutex_unlock(&se->mt_lock);
402 if (!err) {
403 while (!fuse_session_exited(se))
404 sem_wait(&se->mt_finish);
405 if (se->debug)
406 fuse_log(FUSE_LOG_DEBUG,
407 "fuse: session exited, terminating workers\n");
408
409 pthread_mutex_lock(&se->mt_lock);
410 for (w = mt.main.next; w != &mt.main; w = w->next)
411 pthread_cancel(w->thread_id);
412 pthread_mutex_unlock(&se->mt_lock);
413
414 while (mt.main.next != &mt.main)
415 fuse_join_worker(&mt, mt.main.next);
416
417 err = mt.error;
418
419 if (se->uring.pool)
420 fuse_uring_stop(se);
421 }
422
423 pthread_mutex_destroy(&se->mt_lock);
424 if(se->error != 0)
425 err = se->error;
426
427
428 if (created_config) {
429 fuse_loop_cfg_destroy(config);
430 config = NULL;
431 }
432
433 return err;
434}
435
436int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1);
437FUSE_SYMVER("fuse_session_loop_mt_32", "fuse_session_loop_mt@FUSE_3.2")
438int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1)
439{
440 int err;
441 struct fuse_loop_config *config = NULL;
442
443 if (config_v1 != NULL) {
444 /* convert the given v1 config */
445 config = fuse_loop_cfg_create();
446 if (config == NULL)
447 return ENOMEM;
448
449 fuse_loop_cfg_convert(config, config_v1);
450 }
451
452 err = fuse_session_loop_mt_312(se, config);
453
454 fuse_loop_cfg_destroy(config);
455
456 return err;
457}
458
459
460int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd);
461FUSE_SYMVER("fuse_session_loop_mt_31", "fuse_session_loop_mt@FUSE_3.0")
462int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd)
463{
464 int err;
465 struct fuse_loop_config *config = fuse_loop_cfg_create();
466 if (clone_fd > 0)
467 fuse_loop_cfg_set_clone_fd(config, clone_fd);
468 err = fuse_session_loop_mt_312(se, config);
469
470 fuse_loop_cfg_destroy(config);
471
472 return err;
473}
474
475struct fuse_loop_config *fuse_loop_cfg_create(void)
476{
477 struct fuse_loop_config *config = calloc(1, sizeof(*config));
478 if (config == NULL)
479 return NULL;
480
481 config->version_id = FUSE_LOOP_MT_V2_IDENTIFIER;
482 config->max_idle_threads = FUSE_LOOP_MT_DEF_IDLE_THREADS;
483 config->max_threads = FUSE_LOOP_MT_DEF_MAX_THREADS;
484 config->clone_fd = FUSE_LOOP_MT_DEF_CLONE_FD;
485
486 return config;
487}
488
489void fuse_loop_cfg_destroy(struct fuse_loop_config *config)
490{
491 free(config);
492}
493
494int fuse_loop_cfg_verify(struct fuse_loop_config *config)
495{
496 if (config->version_id != FUSE_LOOP_MT_V2_IDENTIFIER)
497 return -EINVAL;
498
499 return 0;
500}
501
502void fuse_loop_cfg_convert(struct fuse_loop_config *config,
503 struct fuse_loop_config_v1 *v1_conf)
504{
505 fuse_loop_cfg_set_idle_threads(config, v1_conf->max_idle_threads);
506
507 fuse_loop_cfg_set_clone_fd(config, v1_conf->clone_fd);
508}
509
510void fuse_loop_cfg_set_idle_threads(struct fuse_loop_config *config,
511 unsigned int value)
512{
513 if (value > FUSE_LOOP_MT_MAX_THREADS) {
514 if (value != UINT_MAX)
515 fuse_log(FUSE_LOG_ERR,
516 "Ignoring invalid max threads value "
517 "%u > max (%u).\n", value,
518 FUSE_LOOP_MT_MAX_THREADS);
519 return;
520 }
521 config->max_idle_threads = value;
522}
523
524void fuse_loop_cfg_set_max_threads(struct fuse_loop_config *config,
525 unsigned int value)
526{
527 config->max_threads = value;
528}
529
530void fuse_loop_cfg_set_clone_fd(struct fuse_loop_config *config,
531 unsigned int value)
532{
533 config->clone_fd = value;
534}
535
@ FUSE_BUF_IS_FD
void fuse_log(enum fuse_log_level level, const char *fmt,...)
Definition fuse_log.c:77
void fuse_session_exit(struct fuse_session *se)
int fuse_session_exited(struct fuse_session *se)
unsigned int max_threads
Definition fuse_i.h:166
unsigned int max_idle_threads