libfuse: fuse-3.17.4/lib/fuse_loop_mt.c Source File

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 if (!O_CLOEXEC) {
279 res = fcntl(clonefd, F_SETFD, FD_CLOEXEC);
280 if (res == -1) {
281 fuse_log(FUSE_LOG_ERR, "fuse: failed to set CLOEXEC: %s\n",
282 strerror(errno));
283 close(clonefd);
284 return -1;
285 }
286 }
287
288 masterfd = se->fd;
289 res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
290 if (res == -1) {
291 fuse_log(FUSE_LOG_ERR, "fuse: failed to clone device fd: %s\n",
292 strerror(errno));
293 close(clonefd);
294 return -1;
295 }
296 return clonefd;
297}
298
299static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
300{
301 int clonefd;
302 struct fuse_session *se = mt->se;
303 struct fuse_chan *newch;
304
305 if (se->io != NULL) {
306 if (se->io->clone_fd != NULL)
307 clonefd = se->io->clone_fd(se->fd);
308 else
309 return NULL;
310 } else {
311 clonefd = fuse_clone_chan_fd_default(se);
312 }
313 if (clonefd < 0)
314 return NULL;
315
316 newch = fuse_chan_new(clonefd);
317 if (newch == NULL)
318 close(clonefd);
319
320 return newch;
321}
322
323static int fuse_loop_start_thread(struct fuse_mt *mt)
324{
325 int res;
326
327 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
328 if (!w) {
329 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate worker structure\n");
330 return -1;
331 }
332 memset(w, 0, sizeof(struct fuse_worker));
333 w->fbuf.mem = NULL;
334 w->mt = mt;
335
336 w->ch = NULL;
337 if (mt->clone_fd) {
338 w->ch = fuse_clone_chan(mt);
339 if(!w->ch) {
340 /* Don't attempt this again */
341 fuse_log(FUSE_LOG_ERR, "fuse: trying to continue "
342 "without -o clone_fd.\n");
343 mt->clone_fd = 0;
344 }
345 }
346
347 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
348 if (res == -1) {
349 fuse_chan_put(w->ch);
350 free(w);
351 return -1;
352 }
353 list_add_worker(w, &mt->main);
354 mt->numavail ++;
355 mt->numworker ++;
356
357 return 0;
358}
359
360static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
361{
362 pthread_join(w->thread_id, NULL);
363 pthread_mutex_lock(&mt->lock);
364 list_del_worker(w);
365 pthread_mutex_unlock(&mt->lock);
366 fuse_buf_free(&w->fbuf);
367 fuse_chan_put(w->ch);
368 free(w);
369}
370
371int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config);
372FUSE_SYMVER("fuse_session_loop_mt_312", "fuse_session_loop_mt@@FUSE_3.12")
373int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config)
374{
375int err;
376 struct fuse_mt mt;
377 struct fuse_worker *w;
378 int created_config = 0;
379
380 if (config) {
381 err = fuse_loop_cfg_verify(config);
382 if (err)
383 return err;
384 } else {
385 /* The caller does not care about parameters - use the default */
386 config = fuse_loop_cfg_create();
387 created_config = 1;
388 }
389
390
391 memset(&mt, 0, sizeof(struct fuse_mt));
392 mt.se = se;
393 mt.clone_fd = config->clone_fd;
394 mt.error = 0;
395 mt.numworker = 0;
396 mt.numavail = 0;
397 mt.max_idle = config->max_idle_threads;
398 mt.max_threads = config->max_threads;
399 mt.main.thread_id = pthread_self();
400 mt.main.prev = mt.main.next = &mt.main;
401 sem_init(&mt.finish, 0, 0);
402 pthread_mutex_init(&mt.lock, NULL);
403
404 pthread_mutex_lock(&mt.lock);
405 err = fuse_loop_start_thread(&mt);
406 pthread_mutex_unlock(&mt.lock);
407 if (!err) {
408 /* sem_wait() is interruptible */
409 while (!fuse_session_exited(se))
410 sem_wait(&mt.finish);
411
412 pthread_mutex_lock(&mt.lock);
413 for (w = mt.main.next; w != &mt.main; w = w->next)
414 pthread_cancel(w->thread_id);
415 mt.exit = 1;
416 pthread_mutex_unlock(&mt.lock);
417
418 while (mt.main.next != &mt.main)
419 fuse_join_worker(&mt, mt.main.next);
420
421 err = mt.error;
422 }
423
424 pthread_mutex_destroy(&mt.lock);
425 sem_destroy(&mt.finish);
426 if(se->error != 0)
427 err = se->error;
428 fuse_session_reset(se);
429
430 if (created_config) {
431 fuse_loop_cfg_destroy(config);
432 config = NULL;
433 }
434
435 return err;
436}
437
438int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1);
439FUSE_SYMVER("fuse_session_loop_mt_32", "fuse_session_loop_mt@FUSE_3.2")
440int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1)
441{
442 int err;
443 struct fuse_loop_config *config = NULL;
444
445 if (config_v1 != NULL) {
446 /* convert the given v1 config */
447 config = fuse_loop_cfg_create();
448 if (config == NULL)
449 return ENOMEM;
450
451 fuse_loop_cfg_convert(config, config_v1);
452 }
453
454 err = fuse_session_loop_mt_312(se, config);
455
456 fuse_loop_cfg_destroy(config);
457
458 return err;
459}
460
461
462int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd);
463FUSE_SYMVER("fuse_session_loop_mt_31", "fuse_session_loop_mt@FUSE_3.0")
464int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd)
465{
466 int err;
467 struct fuse_loop_config *config = fuse_loop_cfg_create();
468 if (clone_fd > 0)
469 fuse_loop_cfg_set_clone_fd(config, clone_fd);
470 err = fuse_session_loop_mt_312(se, config);
471
472 fuse_loop_cfg_destroy(config);
473
474 return err;
475}
476
477struct fuse_loop_config *fuse_loop_cfg_create(void)
478{
479 struct fuse_loop_config *config = calloc(1, sizeof(*config));
480 if (config == NULL)
481 return NULL;
482
483 config->version_id = FUSE_LOOP_MT_V2_IDENTIFIER;
484 config->max_idle_threads = FUSE_LOOP_MT_DEF_IDLE_THREADS;
485 config->max_threads = FUSE_LOOP_MT_DEF_MAX_THREADS;
486 config->clone_fd = FUSE_LOOP_MT_DEF_CLONE_FD;
487
488 return config;
489}
490
491void fuse_loop_cfg_destroy(struct fuse_loop_config *config)
492{
493 free(config);
494}
495
496int fuse_loop_cfg_verify(struct fuse_loop_config *config)
497{
498 if (config->version_id != FUSE_LOOP_MT_V2_IDENTIFIER)
499 return -EINVAL;
500
501 return 0;
502}
503
504void fuse_loop_cfg_convert(struct fuse_loop_config *config,
505 struct fuse_loop_config_v1 *v1_conf)
506{
507 fuse_loop_cfg_set_idle_threads(config, v1_conf->max_idle_threads);
508
509 fuse_loop_cfg_set_clone_fd(config, v1_conf->clone_fd);
510}
511
512void fuse_loop_cfg_set_idle_threads(struct fuse_loop_config *config,
513 unsigned int value)
514{
515 if (value > FUSE_LOOP_MT_MAX_THREADS) {
516 if (value != UINT_MAX)
517 fuse_log(FUSE_LOG_ERR,
518 "Ignoring invalid max threads value "
519 "%u > max (%u).\n", value,
520 FUSE_LOOP_MT_MAX_THREADS);
521 return;
522 }
523 config->max_idle_threads = value;
524}
525
526void fuse_loop_cfg_set_max_threads(struct fuse_loop_config *config,
527 unsigned int value)
528{
529 config->max_threads = value;
530}
531
532void fuse_loop_cfg_set_clone_fd(struct fuse_loop_config *config,
533 unsigned int value)
534{
535 config->clone_fd = value;
536}
537
FUSE_BUF_IS_FD
@ FUSE_BUF_IS_FD
Definition fuse_common.h:815
fuse_log
void fuse_log(enum fuse_log_level level, const char *fmt,...)
Definition fuse_log.c:77
fuse_session_exit
void fuse_session_exit(struct fuse_session *se)
fuse_session_exited
int fuse_session_exited(struct fuse_session *se)
fuse_session_reset
void fuse_session_reset(struct fuse_session *se)
fuse_buf
Definition fuse_common.h:884
fuse_loop_config
Definition fuse_common.h:147
fuse_loop_config::clone_fd
int clone_fd
Definition fuse_common.h:155
fuse_loop_config::max_threads
unsigned int max_threads
Definition fuse_i.h:166
fuse_loop_config::max_idle_threads
unsigned int max_idle_threads
Definition fuse_common.h:167