blob: 7e400c2a4b4a1dce862835ab88145575bba10da0 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
7*/
8
9#include "fuse_lowlevel.h"
10#include "fuse_misc.h"
11#include "fuse_kernel.h"
Dees_Troye34c1332013-02-06 19:13:00 +000012#include "fuse_i.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050013
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18#include <signal.h>
19#include <semaphore.h>
20#include <errno.h>
21#include <sys/time.h>
Dees_Troye34c1332013-02-06 19:13:00 +000022#include <pthread.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050023
24/* Environment var controlling the thread stack size */
25#define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
26
27struct fuse_worker {
28 struct fuse_worker *prev;
29 struct fuse_worker *next;
30 pthread_t thread_id;
31 size_t bufsize;
32 char *buf;
33 struct fuse_mt *mt;
34};
35
36struct fuse_mt {
37 pthread_mutex_t lock;
38 int numworker;
39 int numavail;
40 struct fuse_session *se;
41 struct fuse_chan *prevch;
42 struct fuse_worker main;
43 sem_t finish;
44 int exit;
45 int error;
46};
47
48static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
49{
50 struct fuse_worker *prev = next->prev;
51 w->next = next;
52 w->prev = prev;
53 prev->next = w;
54 next->prev = w;
55}
56
57static void list_del_worker(struct fuse_worker *w)
58{
59 struct fuse_worker *prev = w->prev;
60 struct fuse_worker *next = w->next;
61 prev->next = next;
62 next->prev = prev;
63}
64
65#define PTHREAD_CANCEL_ENABLE 0
66#define PTHREAD_CANCEL_DISABLE 1
67
Dees_Troye34c1332013-02-06 19:13:00 +000068static int fuse_loop_start_thread(struct fuse_mt *mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050069
70static void *fuse_do_work(void *data)
71{
72 struct fuse_worker *w = (struct fuse_worker *) data;
73 struct fuse_mt *mt = w->mt;
74
75 while (!fuse_session_exited(mt->se)) {
76 int isforget = 0;
77 struct fuse_chan *ch = mt->prevch;
Dees_Troye34c1332013-02-06 19:13:00 +000078 struct fuse_buf fbuf = {
79 .mem = w->buf,
80 .size = w->bufsize,
81 };
bigbiff bigbiff9c754052013-01-09 09:09:08 -050082 int res;
83
84 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
Dees_Troye34c1332013-02-06 19:13:00 +000085 res = fuse_session_receive_buf(mt->se, &fbuf, &ch);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050086 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
87 if (res == -EINTR)
88 continue;
89 if (res <= 0) {
90 if (res < 0) {
91 fuse_session_exit(mt->se);
92 mt->error = -1;
93 }
94 break;
95 }
96
97 pthread_mutex_lock(&mt->lock);
98 if (mt->exit) {
99 pthread_mutex_unlock(&mt->lock);
100 return NULL;
101 }
102
103 /*
104 * This disgusting hack is needed so that zillions of threads
105 * are not created on a burst of FORGET messages
106 */
Dees_Troye34c1332013-02-06 19:13:00 +0000107 if (!(fbuf.flags & FUSE_BUF_IS_FD)) {
108 struct fuse_in_header *in = fbuf.mem;
109
110 if (in->opcode == FUSE_FORGET ||
111 in->opcode == FUSE_BATCH_FORGET)
112 isforget = 1;
113 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500114
115 if (!isforget)
116 mt->numavail--;
117 if (mt->numavail == 0)
Dees_Troye34c1332013-02-06 19:13:00 +0000118 fuse_loop_start_thread(mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500119 pthread_mutex_unlock(&mt->lock);
120
Dees_Troye34c1332013-02-06 19:13:00 +0000121 fuse_session_process_buf(mt->se, &fbuf, ch);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500122
123 pthread_mutex_lock(&mt->lock);
124 if (!isforget)
125 mt->numavail++;
126 if (mt->numavail > 10) {
127 if (mt->exit) {
128 pthread_mutex_unlock(&mt->lock);
129 return NULL;
130 }
131 list_del_worker(w);
132 mt->numavail--;
133 mt->numworker--;
134 pthread_mutex_unlock(&mt->lock);
135
136 pthread_detach(w->thread_id);
137 free(w->buf);
138 free(w);
139 return NULL;
140 }
141 pthread_mutex_unlock(&mt->lock);
142 }
143
144 sem_post(&mt->finish);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145
146 return NULL;
147}
148
Dees_Troye34c1332013-02-06 19:13:00 +0000149int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500150{
151 sigset_t oldset;
152 sigset_t newset;
153 int res;
154 pthread_attr_t attr;
155 char *stack_size;
Dees_Troye34c1332013-02-06 19:13:00 +0000156
157 /* Override default stack size */
158 pthread_attr_init(&attr);
159 stack_size = getenv(ENVNAME_THREAD_STACK);
160 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
161 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
162
163 /* Disallow signal reception in worker threads */
164 sigemptyset(&newset);
165 sigaddset(&newset, SIGTERM);
166 sigaddset(&newset, SIGINT);
167 sigaddset(&newset, SIGHUP);
168 sigaddset(&newset, SIGQUIT);
169 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
170 res = pthread_create(thread_id, &attr, func, arg);
171 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
172 pthread_attr_destroy(&attr);
173 if (res != 0) {
174 fprintf(stderr, "fuse: error creating thread: %s\n",
175 strerror(res));
176 return -1;
177 }
178
179 return 0;
180}
181
182static int fuse_loop_start_thread(struct fuse_mt *mt)
183{
184 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500185 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
186 if (!w) {
187 fprintf(stderr, "fuse: failed to allocate worker structure\n");
188 return -1;
189 }
190 memset(w, 0, sizeof(struct fuse_worker));
191 w->bufsize = fuse_chan_bufsize(mt->prevch);
192 w->buf = malloc(w->bufsize);
193 w->mt = mt;
194 if (!w->buf) {
195 fprintf(stderr, "fuse: failed to allocate read buffer\n");
196 free(w);
197 return -1;
198 }
199
Dees_Troye34c1332013-02-06 19:13:00 +0000200 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
201 if (res == -1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500202 free(w->buf);
203 free(w);
204 return -1;
205 }
206 list_add_worker(w, &mt->main);
207 mt->numavail ++;
208 mt->numworker ++;
209
210 return 0;
211}
212
213static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
214{
215 pthread_join(w->thread_id, NULL);
216 pthread_mutex_lock(&mt->lock);
217 list_del_worker(w);
218 pthread_mutex_unlock(&mt->lock);
219 free(w->buf);
220 free(w);
221}
222
223int fuse_session_loop_mt(struct fuse_session *se)
224{
225 int err;
226 struct fuse_mt mt;
227 struct fuse_worker *w;
228
229 memset(&mt, 0, sizeof(struct fuse_mt));
230 mt.se = se;
231 mt.prevch = fuse_session_next_chan(se, NULL);
232 mt.error = 0;
233 mt.numworker = 0;
234 mt.numavail = 0;
235 mt.main.thread_id = pthread_self();
236 mt.main.prev = mt.main.next = &mt.main;
237 sem_init(&mt.finish, 0, 0);
238 fuse_mutex_init(&mt.lock);
239
240 pthread_mutex_lock(&mt.lock);
Dees_Troye34c1332013-02-06 19:13:00 +0000241 err = fuse_loop_start_thread(&mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500242 pthread_mutex_unlock(&mt.lock);
243 if (!err) {
244 /* sem_wait() is interruptible */
245 while (!fuse_session_exited(se))
246 sem_wait(&mt.finish);
247
248 for (w = mt.main.next; w != &mt.main; w = w->next)
249 pthread_cancel(w->thread_id);
250 mt.exit = 1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500251
252 while (mt.main.next != &mt.main)
253 fuse_join_worker(&mt, mt.main.next);
254
255 err = mt.error;
256 }
257
258 pthread_mutex_destroy(&mt.lock);
259 sem_destroy(&mt.finish);
260 fuse_session_reset(se);
261 return err;
262}