11 #include "fuse_config.h"
12 #include "fuse_lowlevel.h"
13 #include "fuse_misc.h"
14 #include "fuse_kernel.h"
22 #include <semaphore.h>
25 #include <sys/ioctl.h>
30 #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
32 #define FUSE_LOOP_MT_V2_IDENTIFIER INT_MAX - 2
33 #define FUSE_LOOP_MT_DEF_CLONE_FD 0
34 #define FUSE_LOOP_MT_DEF_MAX_THREADS 10
35 #define FUSE_LOOP_MT_DEF_IDLE_THREADS -1
39 #define FUSE_LOOP_MT_MAX_THREADS (100U * 1000)
42 struct fuse_worker *prev;
43 struct fuse_worker *next;
57 struct fuse_session *se;
58 struct fuse_worker main;
67 static struct fuse_chan *fuse_chan_new(
int fd)
69 struct fuse_chan *ch = (
struct fuse_chan *) malloc(
sizeof(*ch));
71 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate channel\n");
75 memset(ch, 0,
sizeof(*ch));
78 pthread_mutex_init(&ch->lock, NULL);
83 struct fuse_chan *fuse_chan_get(
struct fuse_chan *ch)
86 pthread_mutex_lock(&ch->lock);
88 pthread_mutex_unlock(&ch->lock);
93 void fuse_chan_put(
struct fuse_chan *ch)
97 pthread_mutex_lock(&ch->lock);
100 pthread_mutex_unlock(&ch->lock);
102 pthread_mutex_destroy(&ch->lock);
105 pthread_mutex_unlock(&ch->lock);
108 static void list_add_worker(
struct fuse_worker *w,
struct fuse_worker *next)
110 struct fuse_worker *prev = next->prev;
117 static void list_del_worker(
struct fuse_worker *w)
119 struct fuse_worker *prev = w->prev;
120 struct fuse_worker *next = w->next;
125 static int fuse_loop_start_thread(
struct fuse_mt *mt);
127 static void *fuse_do_work(
void *data)
129 struct fuse_worker *w = (
struct fuse_worker *) data;
130 struct fuse_mt *mt = w->mt;
136 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
137 res = fuse_session_receive_buf_int(mt->se, &w->fbuf, w->ch);
138 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
149 pthread_mutex_lock(&mt->lock);
151 pthread_mutex_unlock(&mt->lock);
160 struct fuse_in_header *in = w->fbuf.mem;
162 if (in->opcode == FUSE_FORGET ||
163 in->opcode == FUSE_BATCH_FORGET)
169 if (mt->numavail == 0 && mt->numworker < mt->max_threads)
170 fuse_loop_start_thread(mt);
171 pthread_mutex_unlock(&mt->lock);
173 fuse_session_process_buf_int(mt->se, &w->fbuf, w->ch);
175 pthread_mutex_lock(&mt->lock);
185 if (mt->max_idle != -1 && mt->numavail > mt->max_idle && mt->numworker > 1) {
187 pthread_mutex_unlock(&mt->lock);
193 pthread_mutex_unlock(&mt->lock);
195 pthread_detach(w->thread_id);
197 fuse_chan_put(w->ch);
201 pthread_mutex_unlock(&mt->lock);
204 sem_post(&mt->finish);
209 int fuse_start_thread(pthread_t *thread_id,
void *(*func)(
void *),
void *arg)
221 pthread_attr_init(&attr);
222 stack_size = getenv(ENVNAME_THREAD_STACK);
223 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
224 fuse_log(FUSE_LOG_ERR,
"fuse: invalid stack size: %s\n", stack_size);
227 sigemptyset(&newset);
228 sigaddset(&newset, SIGTERM);
229 sigaddset(&newset, SIGINT);
230 sigaddset(&newset, SIGHUP);
231 sigaddset(&newset, SIGQUIT);
232 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
233 res = pthread_create(thread_id, &attr, func, arg);
234 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
235 pthread_attr_destroy(&attr);
237 fuse_log(FUSE_LOG_ERR,
"fuse: error creating thread: %s\n",
245 static struct fuse_chan *fuse_clone_chan(
struct fuse_mt *mt)
250 struct fuse_chan *newch;
251 const char *devname =
"/dev/fuse";
256 clonefd = open(devname, O_RDWR | O_CLOEXEC);
258 fuse_log(FUSE_LOG_ERR,
"fuse: failed to open %s: %s\n", devname,
262 fcntl(clonefd, F_SETFD, FD_CLOEXEC);
264 masterfd = mt->se->fd;
265 res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
267 fuse_log(FUSE_LOG_ERR,
"fuse: failed to clone device fd: %s\n",
272 newch = fuse_chan_new(clonefd);
279 static int fuse_loop_start_thread(
struct fuse_mt *mt)
283 struct fuse_worker *w = malloc(
sizeof(
struct fuse_worker));
285 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate worker structure\n");
288 memset(w, 0,
sizeof(
struct fuse_worker));
294 w->ch = fuse_clone_chan(mt);
297 fuse_log(FUSE_LOG_ERR,
"fuse: trying to continue "
298 "without -o clone_fd.\n");
303 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
305 fuse_chan_put(w->ch);
309 list_add_worker(w, &mt->main);
316 static void fuse_join_worker(
struct fuse_mt *mt,
struct fuse_worker *w)
318 pthread_join(w->thread_id, NULL);
319 pthread_mutex_lock(&mt->lock);
321 pthread_mutex_unlock(&mt->lock);
323 fuse_chan_put(w->ch);
327 int fuse_session_loop_mt_312(
struct fuse_session *se,
struct fuse_loop_config *config);
328 FUSE_SYMVER(
"fuse_session_loop_mt_312",
"fuse_session_loop_mt@@FUSE_3.12")
329 int fuse_session_loop_mt_312(struct fuse_session *se, struct
fuse_loop_config *config)
333 struct fuse_worker *w;
334 int created_config = 0;
337 err = fuse_loop_cfg_verify(config);
347 memset(&mt, 0,
sizeof(
struct fuse_mt));
349 mt.clone_fd = config->clone_fd;
353 mt.max_idle = config->max_idle_threads;
354 mt.max_threads = config->max_threads;
355 mt.main.thread_id = pthread_self();
356 mt.main.prev = mt.main.next = &mt.main;
357 sem_init(&mt.finish, 0, 0);
358 pthread_mutex_init(&mt.lock, NULL);
360 pthread_mutex_lock(&mt.lock);
361 err = fuse_loop_start_thread(&mt);
362 pthread_mutex_unlock(&mt.lock);
366 sem_wait(&mt.finish);
368 pthread_mutex_lock(&mt.lock);
369 for (w = mt.main.next; w != &mt.main; w = w->next)
370 pthread_cancel(w->thread_id);
372 pthread_mutex_unlock(&mt.lock);
374 while (mt.main.next != &mt.main)
375 fuse_join_worker(&mt, mt.main.next);
380 pthread_mutex_destroy(&mt.lock);
381 sem_destroy(&mt.finish);
386 if (created_config) {
394 int fuse_session_loop_mt_32(
struct fuse_session *se,
struct fuse_loop_config_v1 *config_v1);
395 FUSE_SYMVER(
"fuse_session_loop_mt_32",
"fuse_session_loop_mt@FUSE_3.2")
396 int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config_v1 *config_v1)
401 if (config_v1 != NULL) {
410 err = fuse_session_loop_mt_312(se, config);
418 int fuse_session_loop_mt_31(
struct fuse_session *se,
int clone_fd);
419 FUSE_SYMVER(
"fuse_session_loop_mt_31",
"fuse_session_loop_mt@FUSE_3.0")
420 int fuse_session_loop_mt_31(struct fuse_session *se,
int clone_fd)
425 return fuse_session_loop_mt_312(se, config);
434 config->version_id = FUSE_LOOP_MT_V2_IDENTIFIER;
436 config->
max_threads = FUSE_LOOP_MT_DEF_MAX_THREADS;
437 config->
clone_fd = FUSE_LOOP_MT_DEF_CLONE_FD;
449 if (config->version_id != FUSE_LOOP_MT_V2_IDENTIFIER)
456 struct fuse_loop_config_v1 *v1_conf)
466 if (value > FUSE_LOOP_MT_MAX_THREADS) {
467 if (value != UINT_MAX)
469 "Ignoring invalid max threads value "
470 "%u > max (%u).\n", value,
471 FUSE_LOOP_MT_MAX_THREADS);
void fuse_loop_cfg_convert(struct fuse_loop_config *config, struct fuse_loop_config_v1 *v1_conf)
void fuse_loop_cfg_set_idle_threads(struct fuse_loop_config *config, unsigned int value)
struct fuse_loop_config * fuse_loop_cfg_create(void)
void fuse_loop_cfg_set_clone_fd(struct fuse_loop_config *config, unsigned int value)
void fuse_loop_cfg_destroy(struct fuse_loop_config *config)
void fuse_loop_cfg_set_max_threads(struct fuse_loop_config *config, unsigned int value)
void fuse_log(enum fuse_log_level level, const char *fmt,...)
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_idle_threads