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