12 #include "fuse_lowlevel.h" 13 #include "fuse_misc.h" 14 #include "fuse_kernel.h" 22 #include <semaphore.h> 25 #include <sys/ioctl.h> 29 #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK" 32 struct fuse_worker *prev;
33 struct fuse_worker *next;
45 struct fuse_session *se;
46 struct fuse_worker main;
54 static struct fuse_chan *fuse_chan_new(
int fd)
56 struct fuse_chan *ch = (
struct fuse_chan *) malloc(
sizeof(*ch));
58 fprintf(stderr,
"fuse: failed to allocate channel\n");
62 memset(ch, 0,
sizeof(*ch));
65 fuse_mutex_init(&ch->lock);
70 struct fuse_chan *fuse_chan_get(
struct fuse_chan *ch)
73 pthread_mutex_lock(&ch->lock);
75 pthread_mutex_unlock(&ch->lock);
80 void fuse_chan_put(
struct fuse_chan *ch)
84 pthread_mutex_lock(&ch->lock);
87 pthread_mutex_unlock(&ch->lock);
89 pthread_mutex_destroy(&ch->lock);
92 pthread_mutex_unlock(&ch->lock);
95 static void list_add_worker(
struct fuse_worker *w,
struct fuse_worker *next)
97 struct fuse_worker *prev = next->prev;
104 static void list_del_worker(
struct fuse_worker *w)
106 struct fuse_worker *prev = w->prev;
107 struct fuse_worker *next = w->next;
112 static int fuse_loop_start_thread(
struct fuse_mt *mt);
114 static void *fuse_do_work(
void *data)
116 struct fuse_worker *w = (
struct fuse_worker *) data;
117 struct fuse_mt *mt = w->mt;
123 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
124 res = fuse_session_receive_buf_int(mt->se, &w->fbuf, w->ch);
125 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
136 pthread_mutex_lock(&mt->lock);
138 pthread_mutex_unlock(&mt->lock);
147 struct fuse_in_header *in = w->fbuf.mem;
149 if (in->opcode == FUSE_FORGET ||
150 in->opcode == FUSE_BATCH_FORGET)
156 if (mt->numavail == 0)
157 fuse_loop_start_thread(mt);
158 pthread_mutex_unlock(&mt->lock);
160 fuse_session_process_buf_int(mt->se, &w->fbuf, w->ch);
162 pthread_mutex_lock(&mt->lock);
165 if (mt->numavail > mt->max_idle) {
167 pthread_mutex_unlock(&mt->lock);
173 pthread_mutex_unlock(&mt->lock);
175 pthread_detach(w->thread_id);
177 fuse_chan_put(w->ch);
181 pthread_mutex_unlock(&mt->lock);
184 sem_post(&mt->finish);
189 int fuse_start_thread(pthread_t *thread_id,
void *(*func)(
void *),
void *arg)
198 pthread_attr_init(&attr);
199 stack_size = getenv(ENVNAME_THREAD_STACK);
200 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
201 fprintf(stderr,
"fuse: invalid stack size: %s\n", stack_size);
204 sigemptyset(&newset);
205 sigaddset(&newset, SIGTERM);
206 sigaddset(&newset, SIGINT);
207 sigaddset(&newset, SIGHUP);
208 sigaddset(&newset, SIGQUIT);
209 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
210 res = pthread_create(thread_id, &attr, func, arg);
211 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
212 pthread_attr_destroy(&attr);
214 fprintf(stderr,
"fuse: error creating thread: %s\n",
222 static struct fuse_chan *fuse_clone_chan(
struct fuse_mt *mt)
227 struct fuse_chan *newch;
228 const char *devname =
"/dev/fuse";
233 clonefd = open(devname, O_RDWR | O_CLOEXEC);
235 fprintf(stderr,
"fuse: failed to open %s: %s\n", devname,
239 fcntl(clonefd, F_SETFD, FD_CLOEXEC);
241 masterfd = mt->se->fd;
242 res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
244 fprintf(stderr,
"fuse: failed to clone device fd: %s\n",
249 newch = fuse_chan_new(clonefd);
256 static int fuse_loop_start_thread(
struct fuse_mt *mt)
260 struct fuse_worker *w = malloc(
sizeof(
struct fuse_worker));
262 fprintf(stderr,
"fuse: failed to allocate worker structure\n");
265 memset(w, 0,
sizeof(
struct fuse_worker));
271 w->ch = fuse_clone_chan(mt);
274 fprintf(stderr,
"fuse: trying to continue " 275 "without -o clone_fd.\n");
280 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
282 fuse_chan_put(w->ch);
286 list_add_worker(w, &mt->main);
293 static void fuse_join_worker(
struct fuse_mt *mt,
struct fuse_worker *w)
295 pthread_join(w->thread_id, NULL);
296 pthread_mutex_lock(&mt->lock);
298 pthread_mutex_unlock(&mt->lock);
300 fuse_chan_put(w->ch);
304 FUSE_SYMVER(
".symver fuse_session_loop_mt_32,fuse_session_loop_mt@@FUSE_3.2");
305 int fuse_session_loop_mt_32(
struct fuse_session *se,
struct fuse_loop_config *config)
309 struct fuse_worker *w;
311 memset(&mt, 0,
sizeof(
struct fuse_mt));
318 mt.main.thread_id = pthread_self();
319 mt.main.prev = mt.main.next = &mt.main;
320 sem_init(&mt.finish, 0, 0);
321 fuse_mutex_init(&mt.lock);
323 pthread_mutex_lock(&mt.lock);
324 err = fuse_loop_start_thread(&mt);
325 pthread_mutex_unlock(&mt.lock);
329 sem_wait(&mt.finish);
331 pthread_mutex_lock(&mt.lock);
332 for (w = mt.main.next; w != &mt.main; w = w->next)
333 pthread_cancel(w->thread_id);
335 pthread_mutex_unlock(&mt.lock);
337 while (mt.main.next != &mt.main)
338 fuse_join_worker(&mt, mt.main.next);
343 pthread_mutex_destroy(&mt.lock);
344 sem_destroy(&mt.finish);
352 FUSE_SYMVER(
".symver fuse_session_loop_mt_31,fuse_session_loop_mt@FUSE_3.0");
358 return fuse_session_loop_mt_32(se, &config);
void fuse_session_reset(struct fuse_session *se)
unsigned int max_idle_threads
int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd)
int fuse_session_exited(struct fuse_session *se)
void fuse_session_exit(struct fuse_session *se)