You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
5.6 KiB

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