Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | 8b39a9f | 2002-10-25 12:41:16 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB. |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 9 | #include "fuse_i.h" |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 10 | #include "fuse_lowlevel.h" |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 15 | #include <pthread.h> |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 17 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 18 | static pthread_key_t context_key; |
| 19 | static pthread_mutex_t context_lock = PTHREAD_MUTEX_INITIALIZER; |
| 20 | static int context_ref; |
| 21 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 22 | static struct fuse_context *mt_getcontext(void) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 23 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 24 | struct fuse_context *ctx; |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 26 | ctx = (struct fuse_context *) pthread_getspecific(context_key); |
| 27 | if (ctx == NULL) { |
| 28 | ctx = (struct fuse_context *) malloc(sizeof(struct fuse_context)); |
| 29 | if (ctx == NULL) { |
| 30 | fprintf(stderr, "fuse: failed to allocate thread specific data\n"); |
| 31 | return NULL; |
| 32 | } |
| 33 | pthread_setspecific(context_key, ctx); |
| 34 | } |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 35 | return ctx; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void mt_freecontext(void *data) |
| 39 | { |
| 40 | free(data); |
| 41 | } |
| 42 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 43 | static int mt_create_context_key(void) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 44 | { |
| 45 | int err = 0; |
| 46 | pthread_mutex_lock(&context_lock); |
| 47 | if (!context_ref) { |
| 48 | err = pthread_key_create(&context_key, mt_freecontext); |
| 49 | if (err) |
| 50 | fprintf(stderr, "fuse: failed to create thread specific key: %s\n", |
| 51 | strerror(err)); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 52 | else |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 53 | fuse_set_getcontext_func(mt_getcontext); |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 54 | } |
| 55 | if (!err) |
| 56 | context_ref ++; |
| 57 | pthread_mutex_unlock(&context_lock); |
| 58 | return err; |
| 59 | } |
| 60 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 61 | static void mt_delete_context_key(void) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 62 | { |
| 63 | pthread_mutex_lock(&context_lock); |
| 64 | context_ref--; |
| 65 | if (!context_ref) { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 66 | fuse_set_getcontext_func(NULL); |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 67 | pthread_key_delete(context_key); |
| 68 | } |
| 69 | pthread_mutex_unlock(&context_lock); |
| 70 | } |
| 71 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 72 | struct procdata { |
| 73 | struct fuse *f; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 74 | struct fuse_chan *prevch; |
| 75 | struct fuse_session *prevse; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 76 | fuse_processor_t proc; |
| 77 | void *data; |
| 78 | }; |
| 79 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 80 | static void mt_session_proc(void *data, const char *buf, size_t len, |
| 81 | struct fuse_chan *ch) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 82 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 83 | struct procdata *pd = (struct procdata *) data; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 84 | struct fuse_cmd *cmd = *(struct fuse_cmd **) buf; |
| 85 | |
| 86 | (void) len; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 87 | cmd->ch = ch; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 88 | pd->proc(pd->f, cmd, pd->data); |
| 89 | } |
| 90 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 91 | static void mt_session_exit(void *data, int val) |
| 92 | { |
| 93 | struct procdata *pd = (struct procdata *) data; |
| 94 | if (val) |
| 95 | fuse_session_exit(pd->prevse); |
| 96 | else |
| 97 | fuse_session_reset(pd->prevse); |
| 98 | } |
| 99 | |
| 100 | static int mt_session_exited(void *data) |
| 101 | { |
| 102 | struct procdata *pd = (struct procdata *) data; |
| 103 | return fuse_session_exited(pd->prevse); |
| 104 | } |
| 105 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 106 | static int mt_chan_receive(struct fuse_chan *ch, char *buf, size_t size) |
| 107 | { |
| 108 | struct fuse_cmd *cmd; |
| 109 | struct procdata *pd = (struct procdata *) fuse_chan_data(ch); |
| 110 | |
| 111 | assert(size >= sizeof(cmd)); |
| 112 | |
| 113 | cmd = fuse_read_cmd(pd->f); |
| 114 | if (cmd == NULL) |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 115 | return 0; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 116 | |
| 117 | *(struct fuse_cmd **) buf = cmd; |
| 118 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 119 | return sizeof(cmd); |
| 120 | } |
| 121 | |
| 122 | static int mt_chan_send(struct fuse_chan *ch, const struct iovec iov[], |
| 123 | size_t count) |
| 124 | { |
| 125 | struct procdata *pd = (struct procdata *) fuse_chan_data(ch); |
| 126 | return fuse_chan_send(pd->prevch, iov, count); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 129 | int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data) |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 130 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 131 | int res; |
| 132 | struct procdata pd; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 133 | struct fuse_session *prevse = fuse_get_session(f); |
| 134 | struct fuse_session *se; |
| 135 | struct fuse_chan *prevch = fuse_session_next_chan(prevse, NULL); |
| 136 | struct fuse_chan *ch; |
| 137 | struct fuse_session_ops sop = { |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 138 | .exit = mt_session_exit, |
| 139 | .exited = mt_session_exited, |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 140 | .process = mt_session_proc, |
| 141 | }; |
| 142 | struct fuse_chan_ops cop = { |
| 143 | .receive = mt_chan_receive, |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 144 | .send = mt_chan_send, |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 145 | }; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 146 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 147 | pd.f = f; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 148 | pd.prevch = prevch; |
| 149 | pd.prevse = prevse; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 150 | pd.proc = proc; |
| 151 | pd.data = data; |
| 152 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 153 | se = fuse_session_new(&sop, &pd); |
| 154 | if (se == NULL) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 155 | return -1; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 156 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 157 | ch = fuse_chan_new(&cop, fuse_chan_fd(prevch), sizeof(struct fuse_cmd *), |
| 158 | &pd); |
| 159 | if (ch == NULL) { |
| 160 | fuse_session_destroy(se); |
| 161 | return -1; |
| 162 | } |
| 163 | fuse_session_add_chan(se, ch); |
| 164 | |
| 165 | if (mt_create_context_key() != 0) { |
| 166 | fuse_session_destroy(se); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | res = fuse_session_loop_mt(se); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 171 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 172 | mt_delete_context_key(); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 173 | fuse_session_destroy(se); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 174 | return res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 175 | } |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 176 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 177 | int fuse_loop_mt(struct fuse *f) |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 178 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 179 | int res; |
| 180 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 181 | if (f == NULL) |
| 182 | return -1; |
| 183 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 184 | if (mt_create_context_key() != 0) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 185 | return -1; |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 186 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 187 | res = fuse_session_loop_mt(fuse_get_session(f)); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 188 | |
| 189 | mt_delete_context_key(); |
| 190 | return res; |
Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +0000 | [diff] [blame] | 191 | } |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 192 | |
| 193 | __asm__(".symver fuse_loop_mt_proc,__fuse_loop_mt@"); |