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#ifdef HAVE_PTHREAD_SETNAME_NP
136 pthread_setname_np(pthread_self(), "fuse_worker");
137#endif
138
139 while (!fuse_session_exited(mt->se)) {
140 int isforget = 0;
141 int res;
142
143 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
144 res = fuse_session_receive_buf_internal(mt->se, &w->fbuf,
145 w->ch);
146 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
147 if (res == -EINTR)
148 continue;
149 if (res <= 0) {
150 if (res < 0) {
151 fuse_session_exit(mt->se);
152 mt->error = res;
153 }
154 break;
155 }
156
157 pthread_mutex_lock(&mt->lock);
158 if (mt->exit) {
159 pthread_mutex_unlock(&mt->lock);
160 return NULL;
161 }
162
163 /*
164 * This disgusting hack is needed so that zillions of threads
165 * are not created on a burst of FORGET messages
166 */
167 if (!(w->fbuf.flags & FUSE_BUF_IS_FD)) {
168 struct fuse_in_header *in = w->fbuf.mem;
169
170 if (in->opcode == FUSE_FORGET ||
171 in->opcode == FUSE_BATCH_FORGET)
172 isforget = 1;
173 }
174
175 if (!isforget)
176 mt->numavail--;
177 if (mt->numavail == 0 && mt->numworker < mt->max_threads)
178 fuse_loop_start_thread(mt);
179 pthread_mutex_unlock(&mt->lock);
180
181 fuse_session_process_buf_internal(mt->se, &w->fbuf, w->ch);
182
183 pthread_mutex_lock(&mt->lock);
184 if (!isforget)
185 mt->numavail++;
186
187 /* creating and destroying threads is rather expensive - and there is
188 * not much gain from destroying existing threads. It is therefore
189 * discouraged to set max_idle to anything else than -1. If there
190 * is indeed a good reason to destruct threads it should be done
191 * delayed, a moving average might be useful for that.
192 */
193 if (mt->max_idle != -1 && mt->numavail > mt->max_idle && mt->numworker > 1) {
194 if (mt->exit) {
195 pthread_mutex_unlock(&mt->lock);
196 return NULL;
197 }
198 list_del_worker(w);
199 mt->numavail--;
200 mt->numworker--;
201 pthread_mutex_unlock(&mt->lock);
202
203 pthread_detach(w->thread_id);
204 fuse_buf_free(&w->fbuf);
205 fuse_chan_put(w->ch);
206 free(w);
207 return NULL;
208 }
209 pthread_mutex_unlock(&mt->lock);
210 }
211
212 sem_post(&mt->finish);
213
214 return NULL;
215}
216
217int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
218{
219 sigset_t oldset;
220 sigset_t newset;
221 int res;
222 pthread_attr_t attr;
223 char *stack_size;
224
225 /* Override default stack size
226 * XXX: This should ideally be a parameter option. It is rather
227 * well hidden here.
228 */
229 pthread_attr_init(&attr);
230 stack_size = getenv(ENVNAME_THREAD_STACK);
231 if (stack_size) {
232 long size;
233
234 res = libfuse_strtol(stack_size, &size);
235 if (res)
236 fuse_log(FUSE_LOG_ERR, "fuse: invalid stack size: %s\n",
237 stack_size);
238 else if (pthread_attr_setstacksize(&attr, size))
239 fuse_log(FUSE_LOG_ERR, "fuse: could not set stack size: %ld\n",
240 size);
241 }
242
243 /* Disallow signal reception in worker threads */
244 sigemptyset(&newset);
245 sigaddset(&newset, SIGTERM);
246 sigaddset(&newset, SIGINT);
247 sigaddset(&newset, SIGHUP);
248 sigaddset(&newset, SIGQUIT);
249 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
250 res = pthread_create(thread_id, &attr, func, arg);
251 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
252 pthread_attr_destroy(&attr);
253 if (res != 0) {
254 fuse_log(FUSE_LOG_ERR, "fuse: error creating thread: %s\n",
255 strerror(res));
256 return -1;
257 }
258
259 return 0;
260}
261
262static int fuse_clone_chan_fd_default(struct fuse_session *se)
263{
264 int res;
265 int clonefd;
266 uint32_t masterfd;
267 const char *devname = "/dev/fuse";
268
269#ifndef O_CLOEXEC
270#define O_CLOEXEC 0
271#endif
272 clonefd = open(devname, O_RDWR | O_CLOEXEC);
273 if (clonefd == -1) {
274 fuse_log(FUSE_LOG_ERR, "fuse: failed to open %s: %s\n", devname,
275 strerror(errno));
276 return -1;
277 }
278#ifndef O_CLOEXEC
279 fcntl(clonefd, F_SETFD, FD_CLOEXEC);
280#endif
281
282 masterfd = se->fd;
283 res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
284 if (res == -1) {
285 fuse_log(FUSE_LOG_ERR, "fuse: failed to clone device fd: %s\n",
286 strerror(errno));
287 close(clonefd);
288 return -1;
289 }
290 return clonefd;
291}
292
293static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
294{
295 int clonefd;
296 struct fuse_session *se = mt->se;
297 struct fuse_chan *newch;
298
299 if (se->io != NULL) {
300 if (se->io->clone_fd != NULL)
301 clonefd = se->io->clone_fd(se->fd);
302 else
303 return NULL;
304 } else {
305 clonefd = fuse_clone_chan_fd_default(se);
306 }
307 if (clonefd < 0)
308 return NULL;
309
310 newch = fuse_chan_new(clonefd);
311 if (newch == NULL)
312 close(clonefd);
313
314 return newch;
315}
316
317static int fuse_loop_start_thread(struct fuse_mt *mt)
318{
319 int res;
320
321 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
322 if (!w) {
323 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate worker structure\n");
324 return -1;
325 }
326 memset(w, 0, sizeof(struct fuse_worker));
327 w->fbuf.mem = NULL;
328 w->mt = mt;
329
330 w->ch = NULL;
331 if (mt->clone_fd) {
332 w->ch = fuse_clone_chan(mt);
333 if(!w->ch) {
334 /* Don't attempt this again */
335 fuse_log(FUSE_LOG_ERR, "fuse: trying to continue "
336 "without -o clone_fd.\n");
337 mt->clone_fd = 0;
338 }
339 }
340
341 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
342 if (res == -1) {
343 fuse_chan_put(w->ch);
344 free(w);
345 return -1;
346 }
347 list_add_worker(w, &mt->main);
348 mt->numavail ++;
349 mt->numworker ++;
350
351 return 0;
352}
353
354static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
355{
356 pthread_join(w->thread_id, NULL);
357 pthread_mutex_lock(&mt->lock);
358 list_del_worker(w);
359 pthread_mutex_unlock(&mt->lock);
360 fuse_buf_free(&w->fbuf);
361 fuse_chan_put(w->ch);
362 free(w);
363}
364
365int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config);
366FUSE_SYMVER("fuse_session_loop_mt_312", "fuse_session_loop_mt@@FUSE_3.12")
367int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config)
368{
369int err;
370 struct fuse_mt mt;
371 struct fuse_worker *w;
372 int created_config = 0;
373
374 if (config) {
375 err = fuse_loop_cfg_verify(config);
376 if (err)
377 return err;
378 } else {
379 /* The caller does not care about parameters - use the default */
380 config = fuse_loop_cfg_create();
381 created_config = 1;
382 }
383
384
385 memset(&mt, 0, sizeof(struct fuse_mt));
386 mt.se = se;
387 mt.clone_fd = config->clone_fd;
388 mt.error = 0;
389 mt.numworker = 0;
390 mt.numavail = 0;
391 mt.max_idle = config->max_idle_threads;
392 mt.max_threads = config->max_threads;
393 mt.main.thread_id = pthread_self();
394 mt.main.prev = mt.main.next = &mt.main;
395 sem_init(&mt.finish, 0, 0);
396 pthread_mutex_init(&mt.lock, NULL);
397
398 pthread_mutex_lock(&mt.lock);
399 err = fuse_loop_start_thread(&mt);
400 pthread_mutex_unlock(&mt.lock);
401 if (!err) {
402 /* sem_wait() is interruptible */
403 while (!fuse_session_exited(se))
404 sem_wait(&mt.finish);
405
406 pthread_mutex_lock(&mt.lock);
407 for (w = mt.main.next; w != &mt.main; w = w->next)
408 pthread_cancel(w->thread_id);
409 mt.exit = 1;
410 pthread_mutex_unlock(&mt.lock);
411
412 while (mt.main.next != &mt.main)
413 fuse_join_worker(&mt, mt.main.next);
414
415 err = mt.error;
416 }
417
418 pthread_mutex_destroy(&mt.lock);
419 sem_destroy(&mt.finish);
420 if(se->error != 0)
421 err = se->error;
423
424 if (created_config) {
425 fuse_loop_cfg_destroy(config);
426 config = NULL;
427 }
428
429 return err;
430}
431
432int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1);
433FUSE_SYMVER("fuse_session_loop_mt_32", "fuse_session_loop_mt@FUSE_3.2")
434int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1)
435{
436 int err;
437 struct fuse_loop_config *config = NULL;
438
439 if (config_v1 != NULL) {
440 /* convert the given v1 config */
441 config = fuse_loop_cfg_create();
442 if (config == NULL)
443 return ENOMEM;
444
445 fuse_loop_cfg_convert(config, config_v1);
446 }
447
448 err = fuse_session_loop_mt_312(se, config);
449
450 fuse_loop_cfg_destroy(config);
451
452 return err;
453}
454
455
456int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd);
457FUSE_SYMVER("fuse_session_loop_mt_31", "fuse_session_loop_mt@FUSE_3.0")
458int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd)
459{
460 int err;
461 struct fuse_loop_config *config = fuse_loop_cfg_create();
462 if (clone_fd > 0)
463 fuse_loop_cfg_set_clone_fd(config, clone_fd);
464 err = fuse_session_loop_mt_312(se, config);
465
466 fuse_loop_cfg_destroy(config);
467
468 return err;
469}
470
471struct fuse_loop_config *fuse_loop_cfg_create(void)
472{
473 struct fuse_loop_config *config = calloc(1, sizeof(*config));
474 if (config == NULL)
475 return NULL;
476
477 config->version_id = FUSE_LOOP_MT_V2_IDENTIFIER;
478 config->max_idle_threads = FUSE_LOOP_MT_DEF_IDLE_THREADS;
479 config->max_threads = FUSE_LOOP_MT_DEF_MAX_THREADS;
480 config->clone_fd = FUSE_LOOP_MT_DEF_CLONE_FD;
481
482 return config;
483}
484
485void fuse_loop_cfg_destroy(struct fuse_loop_config *config)
486{
487 free(config);
488}
489
490int fuse_loop_cfg_verify(struct fuse_loop_config *config)
491{
492 if (config->version_id != FUSE_LOOP_MT_V2_IDENTIFIER)
493 return -EINVAL;
494
495 return 0;
496}
497
498void fuse_loop_cfg_convert(struct fuse_loop_config *config,
499 struct fuse_loop_config_v1 *v1_conf)
500{
501 fuse_loop_cfg_set_idle_threads(config, v1_conf->max_idle_threads);
502
503 fuse_loop_cfg_set_clone_fd(config, v1_conf->clone_fd);
504}
505
506void fuse_loop_cfg_set_idle_threads(struct fuse_loop_config *config,
507 unsigned int value)
508{
509 if (value > FUSE_LOOP_MT_MAX_THREADS) {
510 if (value != UINT_MAX)
511 fuse_log(FUSE_LOG_ERR,
512 "Ignoring invalid max threads value "
513 "%u > max (%u).\n", value,
514 FUSE_LOOP_MT_MAX_THREADS);
515 return;
516 }
517 config->max_idle_threads = value;
518}
519
520void fuse_loop_cfg_set_max_threads(struct fuse_loop_config *config,
521 unsigned int value)
522{
523 config->max_threads = value;
524}
525
526void fuse_loop_cfg_set_clone_fd(struct fuse_loop_config *config,
527 unsigned int value)
528{
529 config->clone_fd = value;
530}
531
@ 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:166
unsigned int max_idle_threads