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