blob: a73c399b7d1221f3f09517d8d7bfb5ef0d5952c4 [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"
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include <signal.h>
18#include <semaphore.h>
19#include <errno.h>
20#include <sys/time.h>
21
22#ifdef __MULTI_THREAD
23
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
68static int fuse_start_thread(struct fuse_mt *mt);
69
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;
78 int res;
79
80 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
81 res = fuse_chan_recv(&ch, w->buf, w->bufsize);
82 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
83 if (res == -EINTR)
84 continue;
85 if (res <= 0) {
86 if (res < 0) {
87 fuse_session_exit(mt->se);
88 mt->error = -1;
89 }
90 break;
91 }
92
93 pthread_mutex_lock(&mt->lock);
94 if (mt->exit) {
95 pthread_mutex_unlock(&mt->lock);
96 return NULL;
97 }
98
99 /*
100 * This disgusting hack is needed so that zillions of threads
101 * are not created on a burst of FORGET messages
102 */
103 if (((struct fuse_in_header *) w->buf)->opcode == FUSE_FORGET)
104 isforget = 1;
105
106 if (!isforget)
107 mt->numavail--;
108 if (mt->numavail == 0)
109 fuse_start_thread(mt);
110 pthread_mutex_unlock(&mt->lock);
111
112 fuse_session_process(mt->se, w->buf, res, ch);
113
114 pthread_mutex_lock(&mt->lock);
115 if (!isforget)
116 mt->numavail++;
117 if (mt->numavail > 10) {
118 if (mt->exit) {
119 pthread_mutex_unlock(&mt->lock);
120 return NULL;
121 }
122 list_del_worker(w);
123 mt->numavail--;
124 mt->numworker--;
125 pthread_mutex_unlock(&mt->lock);
126
127 pthread_detach(w->thread_id);
128 free(w->buf);
129 free(w);
130 return NULL;
131 }
132 pthread_mutex_unlock(&mt->lock);
133 }
134
135 sem_post(&mt->finish);
136 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
137 pause();
138
139 return NULL;
140}
141
142static int fuse_start_thread(struct fuse_mt *mt)
143{
144 sigset_t oldset;
145 sigset_t newset;
146 int res;
147 pthread_attr_t attr;
148 char *stack_size;
149 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
150 if (!w) {
151 fprintf(stderr, "fuse: failed to allocate worker structure\n");
152 return -1;
153 }
154 memset(w, 0, sizeof(struct fuse_worker));
155 w->bufsize = fuse_chan_bufsize(mt->prevch);
156 w->buf = malloc(w->bufsize);
157 w->mt = mt;
158 if (!w->buf) {
159 fprintf(stderr, "fuse: failed to allocate read buffer\n");
160 free(w);
161 return -1;
162 }
163
164 /* Override default stack size */
165 pthread_attr_init(&attr);
166 stack_size = getenv(ENVNAME_THREAD_STACK);
167 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
168 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
169
170 /* Disallow signal reception in worker threads */
171 sigemptyset(&newset);
172 sigaddset(&newset, SIGTERM);
173 sigaddset(&newset, SIGINT);
174 sigaddset(&newset, SIGHUP);
175 sigaddset(&newset, SIGQUIT);
176 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
177 res = pthread_create(&w->thread_id, &attr, fuse_do_work, w);
178 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
179 pthread_attr_destroy(&attr);
180 if (res != 0) {
181 fprintf(stderr, "fuse: error creating thread: %s\n",
182 strerror(res));
183 free(w->buf);
184 free(w);
185 return -1;
186 }
187 list_add_worker(w, &mt->main);
188 mt->numavail ++;
189 mt->numworker ++;
190
191 return 0;
192}
193
194static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
195{
196 pthread_join(w->thread_id, NULL);
197 pthread_mutex_lock(&mt->lock);
198 list_del_worker(w);
199 pthread_mutex_unlock(&mt->lock);
200 free(w->buf);
201 free(w);
202}
203
204int fuse_session_loop_mt(struct fuse_session *se)
205{
206 int err;
207 struct fuse_mt mt;
208 struct fuse_worker *w;
209
210 memset(&mt, 0, sizeof(struct fuse_mt));
211 mt.se = se;
212 mt.prevch = fuse_session_next_chan(se, NULL);
213 mt.error = 0;
214 mt.numworker = 0;
215 mt.numavail = 0;
216 mt.main.thread_id = pthread_self();
217 mt.main.prev = mt.main.next = &mt.main;
218 sem_init(&mt.finish, 0, 0);
219 fuse_mutex_init(&mt.lock);
220
221 pthread_mutex_lock(&mt.lock);
222 err = fuse_start_thread(&mt);
223 pthread_mutex_unlock(&mt.lock);
224 if (!err) {
225 /* sem_wait() is interruptible */
226 while (!fuse_session_exited(se))
227 sem_wait(&mt.finish);
228
229 for (w = mt.main.next; w != &mt.main; w = w->next)
230 pthread_cancel(w->thread_id);
231 mt.exit = 1;
232 pthread_mutex_unlock(&mt.lock);
233
234 while (mt.main.next != &mt.main)
235 fuse_join_worker(&mt, mt.main.next);
236
237 err = mt.error;
238 }
239
240 pthread_mutex_destroy(&mt.lock);
241 sem_destroy(&mt.finish);
242 fuse_session_reset(se);
243 return err;
244}
245
246#endif